String representation of an identifier's name

Erik Max Francis max at
Mon Jan 13 00:56:12 CET 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 /
 __ San Jose, CA, USA / 37 20 N 121 53 W / &tSftDotIotE
/  \ A good indignation brings out all one's powers.
\__/ Ralph Waldo Emerson
    Lsystem /
 A Lindenmayer systems explorer in Python.

More information about the Python-list mailing list