How to get the 'name' of an int

Erik Max Francis max at alcyone.com
Fri Feb 28 04:56:40 EST 2003


Hans Brand wrote:

> I would like to get the 'name' of a variable to do something like
> this:
> 
> causea = 12
> causeb = 13
> for cause in (causea, causeb):
>     print "%s = %s" %(name(cause), cause)
> 
> The result should be:
> causea = 12
> causeb = 13
> 
> How can I achieve this?

There is no guaranteed way to do this, as names are not unique, only
objects are.  Consider:

	causea = causeb = 11

If you ask for the name referring to 11, what should it be then?

The long answer is that you might be able to get what you want by
checking the values of the locals/globals dictionary and then seeing
what keys map to them.  The short answer is that if you're trying to do
this you're probably operating under the influence of a design flaw
that's making you think this is a good idea.

Why do you think you want this?

-- 
 Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
 __ San Jose, CA, USA / 37 20 N 121 53 W / &tSftDotIotE
/  \ Eppur, si muove!  (But still it moves!)
\__/ Galileo Galilei
    ZOE / http://www.alcyone.com/pyos/zoe/
 A simple Python OpenGL rendering engine.




More information about the Python-list mailing list