Determining if an object is a class?

Clay_Culver at yahoo.com Clay_Culver at yahoo.com
Wed Jul 12 16:45:34 EDT 2006


I need to find out if an object is a class.  Using new style classes
this is very easy:

class Test(object): pass

obj = Test
   or
obj = Test()

if type(obj) == type:
    # this is a class object..
else:
    # this not a class object

But this fails for old style classes.  For example:

class OldStyleObject: pass

>>> type(OldStyleObject)
<type 'classobj'>

But I have no way of comparing the return value to anything:
>>> type(OldStyleObject) == classobj
Error, classobj not defined!

Since this is all duck typed, I have no idea what the class will be
passed in as, so I can't just compare the raw object.  I have a
solution for this:

if str(type(Test)).find('classobj') != -1:
    # this is a class object

Which is quite simply awful...does anyone know of a better way to do
this?




More information about the Python-list mailing list