Determining if an object is a class?

Michael Spencer mahs at telcopartners.com
Thu Jul 13 12:44:36 EDT 2006


Michele Simionato wrote:
> Clay_Culver at yahoo.com wrote:
>> I need to find out if an object is a class.
>> Which is quite simply awful...does anyone know of a better way to do
>> this?
> 
> inspect.isclass
> 
>      M.S.
> 
...which made me wonder what this canonical test is.  The answer:

def isclass(object):
     """Return true if the object is a class.

     Class objects provide these attributes:
         __doc__         documentation string
         __module__      name of module in which this class was defined"""
     return isinstance(object, types.ClassType) or hasattr(object, '__bases__')

I wonder why the test isn't

     isinstance(object, (types.ClassType, type))

Michael




More information about the Python-list mailing list