Determine an object is a subclass of another

Neil Cerutti horpner at yahoo.com
Tue Jan 9 09:43:00 EST 2007


On 2007-01-09, abcd <codecraig at gmail.com> wrote:
> How can tell if an object is a subclass of something else?
>
> Imagine...
>
> class Thing:
>     pass
>
> class Animal:
>     pass
>
> class Dog:
>     pass
>
> d = Dog()
>
> I want to find out that 'd' is a Dog, Animal and Thing.  Such
> as...
>
> d is a Dog
> d is a Animal
> d is a Thing

isinstance(d, Dog)
isinstance(d, Animal)
isinstance(d, Thing)

Note that in your example d is not an instance of anything but
Dog. If you want a hierarchy, you must say so. Python doesn't
even try to make educated guesses.

class Thing:
  pass

class Animal(Thing):
  pass

class Dog(Animal):
  pass

-- 
Neil Cerutti



More information about the Python-list mailing list