views:

281

answers:

1

Given an object, how do I tell if it's a class, and a subclass of a given class Foo?

e.g.

class Bar(Foo):
  pass

isinstance(Bar(), Foo) # => True

issubclass(Bar, Foo) # <--- how do I do that?
+9  A: 

It works exactly as one would expect it to work...

class Foo():
    pass

class Bar(Foo):
    pass

class Bar2():
    pass

print issubclass(Bar, Foo)  # True
print issubclass(Bar2, Foo) # False

If you want to know if an instance of a class derived from a given base class, you could use:

bar_instance = Bar()
print issubclass(bar_instance.__class__, Foo)
ChristopheD