How to print the name of a list?
Lonnie Princehouse
finite.automaton at gmail.com
Mon Aug 22 14:12:12 EDT 2005
In general, no --- there is no unique mapping between references and
names.
For debugging, however, it is sometimes useful to try this kind of
reverse lookup by iterating over the global dictionary:
def guess_name(thing):
for name, reference in globals.iteritems():
if thing is reference:
return name
else:
return None
# example use:
foo = [1, 2, 3]
bar = [4, 5, 6]
baz = [foo, bar]
for thing in baz:
print guess_name(thing)
# prints "foo" and "bar"
I should emphasize that this is NOT failsafe, and it shouldn't be used
in any way except for debugging IMHO. If you need to associate names
with objects in a guaranteed way, use a dictionary.
More information about the Python-list
mailing list