String representation of an identifier's name

Erik Max Francis max at alcyone.com
Sun Jan 12 18:56:12 EST 2003


Boethius wrote:

> I think it easier to explain with an example, so here it goes:
> Assuming there's a function fn that does what I want, it would work
> like this:
> 
> g = 23
> foo = {'a':56}
> 
> fn(g) => 'g'
> fn(foo) => 'foo'
> 
> How can I write fn? O:-)

The pressing question should really be why you think you need this.  In
Python, variables are really bindings, and as such as not unique; that's
a good sign that you're probably taking the wrong approach by thinking
you want _the_ name of an object, since there's no guaranteeing it has a
unique name.

So back up a step:  What made you conclude that you needed such a
function?  If you tell us about more about the problem that led you to
this solution, you're pretty much guaranteed we can suggested a better
(more "Pythonic") approach.

-- 
 Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
 __ San Jose, CA, USA / 37 20 N 121 53 W / &tSftDotIotE
/  \ A good indignation brings out all one's powers.
\__/ Ralph Waldo Emerson
    Lsystem / http://www.alcyone.com/pyos/lsystem/
 A Lindenmayer systems explorer in Python.




More information about the Python-list mailing list