converting reference name to string ?

Raymond Hettinger python at rcn.com
Fri Mar 15 07:52:11 EST 2002


Since objects can be bound to more than one variable,
the question, what is my name, doesn't translate to
what variables am I bound to.

The .__name__ attribute can tell you where something
was created but not what it is bound to.

In your example,
>>> def a(): hello
>>> d = a
>>> d.__name__   # surprise!
'a'                          # this is where it was defined, not where its
bound

There is a way to find out which variables an object is bound to;
however, it is both arcane and evil in its intent.  Generally, if an
object needs to know what things refer to it, then there is a design
flaw.


Raymond



"tleduc" <thibaut.leduc at ac-lille.fr> wrote in message
news:14b2057b.0203150254.2480f7ae at posting.google.com...
> example:
>
> def a():
>   print 'hello'
>
> ==> string is   a.__name__
>
> but for instances:
>
> class myclass:
>   ....etc....
>
> b = myclass()
> c = myclass()
>
> in execution of this script, how do i get reference string 'a' 'b' or 'c'
?
>
> Thanks in advance.
>
> Thibaut





More information about the Python-list mailing list