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

Josef Dalcolmo dalcolmo at
Thu Jul 22 14:23:05 CEST 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