How to obtain an instance's name at runtime?

Thomas Heller thomas.heller at ion-tof.com
Wed Jun 23 10:45:04 EDT 1999


> I would like to do the following:
> 
> >>> 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__
> >>>

Huh?

Python 1.5.2 (#0, Apr 13 1999, 10:51:12) [MSC 32 bit (Intel)] on win32
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> class C:
...   pass
...
>>> c = C()
>>> c.__class__.__name__
'C'
>>> C.__name__
'C'
>>> c.__name__
Traceback (innermost last):
  File "<stdin>", line 1, in ?
AttributeError: __name__
>>> C.__name__
'C'
>>>
Use c.__class__.__name__!

Thomas Heller
ION-TOF GmbH





More information about the Python-list mailing list