Finding the name of a class

Ryan Paul segphault at sbcglobal.net
Wed May 12 20:34:17 EDT 2004


On Wed, 12 May 2004 20:10:07 -0400, 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.


I havent found any better way to do it:

def classname(c):
    sl = `c`.split(' ')[0][1:].split('.')
    return len(sl) > 1 and sl[1] or sl[0]



More information about the Python-list mailing list