Faqts : Computers : Programming : Languages : Python : Common Problems : Debugging

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

14 of 18 people (78%) answered Yes
Recently 7 of 10 people (70%) answered Yes

Entry

I want to print debugging info but get tired of saying print( 'a: ', a). How can I automatically get the name of a variable I passed?
Introspective: How to find out the names of variables and objects
How can I find all the names a given thing is known under?

Sep 24th, 2009 15:33
Scott Mandarich, Joe Bloggs, Wolfgang Lipp, Luiz Henrique de Figueiredo (on the lua list), Nathan Wallace (http://www.e-gineer.com) (on python.faqts.com) http://www.motogp.com/en/user/561500


import sys
# thx to Luiz Henrique de Figueiredo <[email protected]>
# and  Nathan Wallace (http://www.e-gineer.com) 
# for pointing out this solution in Lua and Python
# -wl
#  names()
#          + thing thing whose names we are looking for  
#          |      + if true, return all the names of <thing>; 
#          |      | otherwise, only the first name found   
#          |      |        + if true, exclude uninteresting 
#          |      |        | internal names (as given in 
#          |      |        | the tuple)
#          |      |        |
def names( thing, all = 1, exclude_internals = 1 ):
    """
    The function names( thing ) will return a list containing
    all the names a thing can be accessed under, or None, if
    it is an anonymous instance. It will never return an empty
    list; all names in the list will be unique.
    See the source for refinements. In particular, it is
    possible to ask for names( thing, all = 0 ), which will
    either return one *string* containing the first name found
    while searching, or None.
    An extension would be to include the module names with the
    results, preferrably in the form 'module.name'. Later.
    wolf <[email protected]>
    """
    # Add <name> to dictionary <names>, conditionally 
    # excluding it in case it is None or on the exclusion 
    # list.
    # Return 1 if name was added to dictionary, 0 otherwise. 
    def add( names, name, exclude_internals = 1 ):
        if exclude_internals:
            if name in ( 
                'S_IROTH', 'ST_UID', 'P_DETACH', 
                'SIGILL', 'R_OK' 
            ):
                name = None
        if name != None: 
            names[ name ] = 1 # or any other dummy value
            return 1
        else:
            return 0
    R = {}
    #   Look for the __name__ attribute and conditionally 
    #   add it to the names collection; in case thing has 
    #   a __name__, it is not to be excluded and we
    #   don't want to have all the names (ie if all = 0), 
    #   then that's it. Otherwise, proceed below.
    #
    #   PS. put the if clause onto a single line, 
    #   that is much clearer.  
    if add( 
        R, 
        getattr( thing, '__name__', None ), 
        exclude_internals 
     ) and not all: 
        return R.keys()[0]
    #   Go through all the modules and check all the 
    #   items recorded there. 
    #   If item is identical to thing, then try to 
    #   add that name to the names collection. As 
    #   above, in case thing has a __name__, it is 
    #   not to be excluded and we don't want to have 
    #   all the names (ie if all = 0), then that's 
    #   it. Otherwise, proceed with the
    #   subsequent items and the subsequent modules. 
    for module in sys.modules.values():
        for name, val in module.__dict__.items():
            if val is thing: 
                if add( R, name, exclude_internals ) and not all: 
                    return R.keys()[0]
    #   Do not return an empty list of keys of the 
    #   names collection, return plain None instead. 
    if len( R.keys() ):
        return R.keys()
    else:
        return None
if __name__ == '__main__':
    sin = sys.stdin
    mymod = sys
    print names( sys.stdin     ) 
    #   ['stdin', '__stdin__', 'sin']
    print names( sin           ) 
    #   ['stdin', '__stdin__', 'sin']
    print names( sys           ) 
    #   ['mymod', 'sys']
    print names( mymod         ) 
    #   ['mymod', 'sys']
    print names( names         ) 
    #   ['names']
    print names( lambda x: x+1 ) 
    #   ['<lambda>']
    print '-----------------------'
    a = 4
    print names( a, exclude_internals = 0 ) 
    # ['P_DETACH', 'S_IROTH', 'R_OK', 'SIGILL', 'ST_UID', 'a']
    print names( a                        ) 
    # ['a']
    print names( a,               all = 0 ) 
    # a
    print '-----------------------'
    b = 4
    c = 4
    a = b * 9 + 4
    print names( a, exclude_internals = 0 ) 
    # ['a']
    print names( b, exclude_internals = 0 ) 
    # ['S_IROTH', 'ST_UID', 'P_DETACH', 'c', 'b', 
    # 'SIGILL', 'R_OK']
    print names( c, exclude_internals = 0 ) 
    # ['S_IROTH', 'ST_UID', 'P_DETACH', 'c', 'b', 
    # 'SIGILL', 'R_OK']
    print names( a                        ) 
    # ['a']
    print names( b                        ) 
    # ['b', 'c']
    print names( c                        ) 
    # ['b', 'c']
    print '-----------------------'
    class X:
        pass
    x = X()
    print names( X   ) #   ['X']
    print names( X() ) #   None
    print names( x   ) #   ['x']
======================================
http://www.attorneyslawyersdirectory.com
http://www.findattorneyslawyers.com
http://www.goodlawyersattorneys.com
http://www.professionallawyersattorneys.com
http://www.bestprofessionallawyers.com
http://www.americalawyersattorneys.com
http://www.availablelawyers.com
http://www.lawyersattorneyslist.com
http://www.lawyersattorneysfirm.com
http://www.originallawyers.com
http://www.lawyersgulf.com
http://www.lawyersband.com
http://www.lawyersdatacenter.com
http://www.lawyerstigers.com
http://www.lawcliff.com
http://www.tidylawyers.com
http://www.lawyersblock.com
http://www.californialawyersattorneys.com
http://www.chicagoattorneyslawyers.com
http://www.goodcalifornialawyers.com
http://www.lawyersattorneysfaqs.com
http://www.divorceattorneyslawyers.com
http://www.duiattorneyslawyers.com
http://www.personalinjurylawyerslist.com
http://www.injurylawyersattorneys.com
http://www.dwilawyersattorneys.com
http://www.healthbigboss.com
http://www.healthdohealth.com
http://www.healthcass.com
http://www.healthsilence.com
http://www.healthpicnic.com
http://www.blackmesothelioma.com
http://www.affordablelawyersattorneys.com
http://www.pickarticles.com
http://www.pickarticle.com
http://www.healthment.com
http://www.healthbyhealth.com
http://www.ehealthebooks.com
http://www.bestproductsofhealth.com
http://www.dietlosingweight.com
http://www.americanehealth.com
http://www.healthydepartment.com
http://www.medicalehealth.com
http://www.healthservicescenter.com
http://www.ehealthenews.com
http://www.ehealthdept.com
http://www.benefitshealthcenter.com
http://www.toplawyersattorneys.com 
http://www.healthxhealth.com
http://mesotheliomakiller.com
http://www.healthinhealth.com
http://www.booksyshop.com
http://www.mesotheliomaout.com
http://www.mesotheliomadown.com
http://www.qwesz.com
http://americanahost.com
http://www.healthfirewall.com
http://www.healthbodyguard.com
http://www.flathealth.com
http://www.computerstan.com
http://www.financestan.com
http://www.technologystan.com
http://www.zobab.com
http://www.businessian.com
http://www.tarabiza.com
http://www.internetstan.com
http://www.moneyenews.com
http://www.hantira.com
http://www.mantofa.com
http://www.tantofa.com
http://www.fantofa.com
http://www.tanpola.com
http://www.tampola.com
http://www.yamot.com
http://www.mozmar.com
http://www.uploadarticles.com
http://www.articlesfreedirectory.com
http://www.articlesxarticles.com
http://www.shikapika.com
http://www.ganazat.com
http://www.stakoza.com
http://www.stupidarticles.com
http://www.damima.com
http://www.halazona.com
http://www.best-cheap-web-hosting.com
======================================