Function that knows its argument's variable name

Helge Stenström h.stenstrom at gmail.com
Sun Mar 14 07:58:07 EDT 2010


I want to write  function that prints a value of a variable, for
debugging. Like:

with

myVariable = "parrot"
otherVariable = "dead"

probe(myVariable)
probe(otherVariable)

instead of the longer

print "myVariable = ", myVariable
print "otherVariable = ", otherVariable

Is that even possible?

The function would look like

def probe(var):
    nameOfVar = someMagic(var)
    print nameOfVar, " = ", var

but can someMagic(var) find the variable name? Is that possible?



More information about the Python-list mailing list