how do you get the name of a dictionary?

sjdevnull at yahoo.com sjdevnull at yahoo.com
Sat Aug 19 01:48:40 EDT 2006


jojoba wrote:
> 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')?

The reason people are banging their heads is because the question
doesn't make sense in general.

If you need to track a name of some sort, the suggestion to have a name
attribute (e.g. your own MyDict class inheriting from dict) may be
reasonable.

But an object may be bound to many names, or none:

Consider:
> Banana = {}
> # I guess you want the name of the dictionary to be "Banana" now?
> Apple = Banana
> # What is the name of the dictionary now?  Apple or Banana?
> Banana = 3
> print Apple
{}
> print Banana
3
> # What is the name of the dictionary now?  Apple?
> Strawberry = [ Apple ]
> Apple = "hello"
> print Strawberry
[ {} ]
> print Apple
hello
> # What is the name of the dictionary now?  It's not bound to any name.




More information about the Python-list mailing list