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.

-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to