Use list name as string

Tino Wildenhain tino at wildenhain.de
Thu Feb 5 10:39:24 CET 2009


Hendrik van Rooyen wrote:
 >  "MRAB" <google at mr....ett.plus.com> wrote:
 >
 >> The actual names of the variables and functions shouldn't matter to the
 >> outside world; the name of an output file shouldn't depend on the name
 >> of a variable.
 >
 > That is a matter of opinion.
 > It is however, an interesting problem, namely:
 >
 > How does one get hold of the actual name by which some parameter
 > is passed?
 >
 > you may want to print, as a debug thingy:
 >
 > print "the name passed in was: ", ImpossibleThingYieldingName
 > print "and it evaluates to:" , ArgumentPassed

This is possible to some degree:

import inspect

def F(a):
     frame_obj,filename,line_no,
     func_name,contextlines,
     contextindex=(inspect.getouterframes(inspect.currentframe()))[1]
         print "F(%s) called from '%s' within '%s' line %d" %
               (repr(a),filename,func_name,line_no)
         for ln,srcline in enumerate(contextlines or []):
             print "%3s : %s" % ('*>' if ln==contextindex else '',srcline)


just play around calling the above function from different
places and you should see what I mean :-)

Regards
Tino
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/x-pkcs7-signature
Size: 3241 bytes
Desc: S/MIME Cryptographic Signature
URL: <http://mail.python.org/pipermail/python-list/attachments/20090205/e43df086/attachment.bin>


More information about the Python-list mailing list