Determining names of instances

Martin von Loewis loewis at informatik.hu-berlin.de
Mon Jan 15 18:34:42 EST 2001


"Daniel Klein" <DanielK at jBASE.com> writes:

> A question I hope has a simple answer. Take the following simple class and
> instance
> 
>     class foobar:
>         pass
> 
>     foo = foobar()
> 
> The question is, is there a way for the object 'foo' to know that its name
> is 'foo'?

No. Objects have no names. Consider

foo = foobar()
bar = foo

What is that object's name? foo or bar? If you still think it is foo,
consider this:

foo = foobar()
bar = foo
foo = foobar()

Now, the object referenced in bar(), should it's "name" still be foo?
Even though the name foo refers to some other object?

Of course, you can invoke globals() and check whether the object is
listed there.

Regards,
Martin




More information about the Python-list mailing list