Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Determine an object is a subclass of another (http://www.velocityreviews.com/forums/t398037-determine-an-object-is-a-subclass-of-another.html)

 abcd 01-09-2007 02:33 PM

Determine an object is a subclass of another

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

 Neil Cerutti 01-09-2007 02:43 PM

Re: Determine an object is a subclass of another

On 2007-01-09, abcd <codecraig@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

 Matimus 01-09-2007 02:46 PM

Re: Determine an object is a subclass of another

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

 abcd 01-09-2007 03:01 PM

Re: Determine an object is a subclass of another

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

 Bruno Desthuilliers 01-09-2007 03:08 PM

Re: Determine an object is a subclass of another

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,

 Felipe Almeida Lessa 01-09-2007 03:44 PM

Re: Determine an object is a subclass of another

On 9 Jan 2007 07:01:31 -0800, abcd <codecraig@gmail.com> 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.

 All times are GMT. The time now is 09:39 AM.