Getting an instance's class name?

Steve Purcell stephen_purcell at yahoo.com
Wed Feb 14 05:31:29 EST 2001


Bjoern Giesler wrote:
> can anyone tell me how to obtain the name(s) of the class(es) that
> constitute an instance's type?
> 

With the '__class__' attribute of the instance, and the '__name__' attribute
of the class:

>>> p = Exception()
>>> p.__class__.__name__
'Exception'
>>> 

Note that you'll get an attribute error for objects that are not class
instances. For non-class instances, a handy way to get the type name is:

>>> p = 1
>>> type(p).__name__
'int'
>>>


-Steve

-- 
Steve Purcell, Pythangelist
http://pyunit.sourceforge.net/
http://pyserv.sourceforge.net/
Available for consulting and training.
"Even snakes are afraid of snakes." -- Steven Wright




More information about the Python-list mailing list