Determine an object is a subclass of another

Discussion in 'Python' started by abcd, Jan 9, 2007.

  1. abcd

    abcd Guest

    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

    Thanks
     
    abcd, Jan 9, 2007
    #1
    1. Advertising

  2. abcd

    Neil Cerutti Guest

    On 2007-01-09, abcd <> 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
     
    Neil Cerutti, Jan 9, 2007
    #2
    1. Advertising

  3. abcd

    Matimus Guest

    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
     
    Matimus, Jan 9, 2007
    #3
  4. abcd

    abcd Guest

    yea i meant to have animal extend thing and dog extend animal....my
    mistake.

    anyways, is there a way to check without having an instance of the
    class?

    such as,

    isinstance(Dog, (Animal, Thing)) ??

    thanks
     
    abcd, Jan 9, 2007
    #4
  5. abcd a écrit :
    > yea i meant to have animal extend thing and dog extend animal....my
    > mistake.
    >
    > anyways, is there a way to check without having an instance of the
    > class?
    >
    > such as,
    >
    > isinstance(Dog, (Animal, Thing)) ??
    >
    >

    issubclass(Dog, Animal)

    Note that such tests should only be used in a very few specific cases -
    certainly not to try&implement some kind of static typing... In Python,
    inheritance is mostly about implementation.
     
    Bruno Desthuilliers, Jan 9, 2007
    #5
  6. On 9 Jan 2007 07:01:31 -0800, abcd <> wrote:
    > anyways, is there a way to check without having an instance of the
    > class?


    In [1]: class A:
    ...: pass
    ...:

    In [2]: class B(A):
    ...: pass
    ...:

    In [3]: issubclass(B, A)
    Out[3]: True

    In [4]: isinstance(B(), B)
    Out[4]: True

    In [5]: isinstance(B(), A)
    Out[5]: True

    --
    Felipe.
     
    Felipe Almeida Lessa, Jan 9, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jstorta
    Replies:
    3
    Views:
    447
    jstorta
    Feb 20, 2006
  2. Peter
    Replies:
    6
    Views:
    258
    Peter
    Jan 5, 2012
  3. S.Volkov
    Replies:
    2
    Views:
    222
    S.Volkov
    Mar 12, 2006
  4. Trans
    Replies:
    8
    Views:
    324
    Robert Klemme
    Oct 23, 2008
  5. Fab

    Subclass of subclass

    Fab, Aug 9, 2012, in forum: C++
    Replies:
    0
    Views:
    397
Loading...

Share This Page