How to obtain an instance's name at runtime?

Frank Niessink frankn=news at cs.vu.nl
Wed Jun 23 10:35:49 EDT 1999


Dinu C. Gherman <gherman at my-deja.com> wrote:

>>>> class C:
> ...    pass
> ...
>>>>
>>>> c = C()
>>>> C.__class__.__name__ # ok
> 'C'
>>>> c.__name__           # not ok
> Traceback (innermost last):
>   File "<interactive input>", line 0, in ?
> AttributeError: __name__
>>>>

> Question: How to obtain an instance's name at runtime?

That instance doesn't have a name. Take for example:

>>> a = b = c = C()

That creates one instance of C, with three references to it. 
Now what is the `real' name of the instance?

Cheers, Frank

-- 
The suit into which the man's body had been stuffed looked as if it's only
purpose in life was to demonstrate how difficult it was to get this sort of
body into a suit.
	-- Douglas Adams, 'The Restaurant at the End of the Universe'




More information about the Python-list mailing list