how do you get the name of a dictionary?

Tim Chase python.list at tim.thechases.com
Fri Aug 18 14:51:57 EDT 2006


> Does anyone know how to find the name of a python data type.
> 
> Conside a dictionary:
> 
> Banana = {}
> 
> Then, how do i ask python for a string representing the name of the
> above dictionary (i.e. 'Banana')?

AFAIK, there's no easy/good way of doing this because that name 
is just a handle to an internal object.  Observe:

 >>> banana = {}
 >>> spatula = banana
 >>> spatula[42] = 'drangle'
 >>> banana
{42: 'drangle'}
 >>> id(spatula)
10304800
 >>> id(banana)
10304800

What does it mean to ask for the name of object ID 10304800? 
it's both "banana" and "spatula".  One might be able to use the 
decompiler libraries and wend one's way through the dark recesses 
of python's internals to extract such information, but this is 
certainly not a beginner's task.

How would you ask for the object?

 >>> print get_name(banana)

you might as well write

 >>> print "banana"

:)

Hope this makes sense...

-tkc





More information about the Python-list mailing list