how do you get the name of a dictionary?

Tim Chase python.list at tim.thechases.com
Fri Aug 18 16:40:21 EDT 2006


>>> for i in dir():
>>>      if eval(i) == Banana:
>>>              print i
>> (sound of head hitting desk)
>>
>> </F>
>>
> lol

As freakish as the solution was, it's not too far off from 
something that actually works (mostly, kinda sorta):

 >>> banana = {}
 >>> spatula = banana
 >>> propane = {}
 >>> [name for name in dir() if id(eval(name)) == id(banana)]
['banana', 'spatula']
 >>> dir()
['__builtins__', '__doc__', '__name__', 'banana', 'name', 
'propane', 'spatula']

While the original just compared on value (and thus, with the 
original "solution", "propane" would also be found), it should be 
pretty safe to compare on id() equality.

Okay, it's semi-obcene in my book, but it's an actual solution in 
a way.

-tkc






More information about the Python-list mailing list