Finding the name of a class

Erik Max Francis max at alcyone.com
Wed May 12 20:37:42 EDT 2004


"Edward C. Jones" wrote:

> Is there a function or method that returns the name of a class or
> class
> instance?
> 
> class X(object):
>      pass
> 
> X.amethod() or X().amethod() should return the string "X".
> 
> X().__class__ returns "<class '__main__.X'>" which I could parse. Ugh.
> 
> Or I could use module pyclbr. Ugh**2.

Use the __name__ attribute:

>>> class C: pass
... 
>>> C.__name__
'C'
>>> C().__class__.__name__
'C'

-- 
 __ Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/
/  \ San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
\__/ Without love, benevolence becomes egotism.
    -- Dr. Martin Luther King, Jr.



More information about the Python-list mailing list