Function that knows its argument's variable name

Helge Stenström h.stenstrom at
Sun Mar 14 12:58:07 CET 2010

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


myVariable = "parrot"
otherVariable = "dead"


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