how can I get the name of a variable (or other object)?

Josef Dalcolmo dalcolmo at vh-s.de
Thu Jul 22 08:23:05 EDT 2004


If I have a Python variable like

var = 33

can I get the name 'var' as a string?

Obviously this does not make much sense when using a single variable, but if I want to print the variable together with it's name, for a list of variables, then it could make sense:

def printvariables(varlist):
....for var in varlist:
........print var.__name__, var

of course the attribute __name__ I just made up, and if this would always return 'var' it would not make any sense either.

I am not sure if such a thing is at all possible in Python.

Best regards - Josef Dalcolmo



More information about the Python-list mailing list