String representation of an identifier's name

Peter Hansen peter at
Sun Jan 12 22:47:54 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:-)

It might help to explain your reasons for needing this.  I don't think
the approach that is probably required would make you happy, unless the 
value of doing this is extremely high.

Usually this sort of thing is needed only in debugging/logging, where
you want to print out the name of an item, along with its value.  If
that's the case, a solution where you pass the name of the item in
to the subroutine *as a string already*, and have the subroutine 
retrieve the value from the calling frame, might be more appropriate.


More information about the Python-list mailing list