name of 'name'

John Roth newsgroups at jhrothjr.com
Mon Mar 22 18:14:55 EST 2004


"Elaine Jackson" <elainejackson7355 at home.com> wrote in message
news:W5G7c.878632$ts4.740941 at pd7tw3no...
> Is there a built-in name for the function that returns an object's name?
That
> is, for
>
> "  lambda x: [y for y in globals().keys() if globals()[y]==x][0]   "  ?
>
> Muchas gracias.

I'm not entirely sure what you're asking for, but if it's
an "official" way of finding out what the module level
names(s) that an object is bound to are, then the answer
is: no, there isn't one. Python does not, in general,
maintain any information that would let you get from
an object to the identifiers it's bound to in any simple
fashion.

There are, of course, a couple of special cases.
Function and class objects both have a __name__
attribute that will tell you what they were bound
to when they were defined. Whether they stayed
there is another question, of course.

John Roth

> Peace
>
>





More information about the Python-list mailing list