Re: How to print the name of a list?
- From: "Lonnie Princehouse" <finite.automaton@xxxxxxxxx>
- Date: 22 Aug 2005 11:12:12 -0700
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.
.
- References:
- How to print the name of a list?
- From: oriana . falco
- How to print the name of a list?
- Prev by Date: Re: network programming
- Next by Date: Re: global interpreter lock
- Previous by thread: How to print the name of a list?
- Next by thread: Re: How to print the name of a list?
- Index(es):
Relevant Pages
|