Determine an object is a subclass of another

Matimus mccredie at gmail.com
Tue Jan 9 15:46:15 CET 2007


First you need to subclass the classes so that Dog actually is a
subclass of Animal which is a subclass of thing...

class Thing:
  pass

class Animal(Thing):
  pass

class Dog(Animal):
  pass

class Weapon(Thing):
  pass

class Gun(Weapon):
  pass

Then you can use 'isinstance'

>>>d = Dog()
>>>isinstance(d,Thing)
True
>>>isinstance(d,Animal)
True
>>>isinstance(d,Weapon)
False
>>>isinstance(d,Gun)
False




More information about the Python-list mailing list