Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Determine an object is a subclass of another

Reply
Thread Tools

Determine an object is a subclass of another

 
 
abcd
Guest
Posts: n/a
 
      01-09-2007
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

 
Reply With Quote
 
 
 
 
Neil Cerutti
Guest
Posts: n/a
 
      01-09-2007
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
 
Reply With Quote
 
 
 
 
Matimus
Guest
Posts: n/a
 
      01-09-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

 
Reply With Quote
 
abcd
Guest
Posts: n/a
 
      01-09-2007
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

 
Reply With Quote
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      01-09-2007
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.

 
Reply With Quote
 
Felipe Almeida Lessa
Guest
Posts: n/a
 
      01-09-2007
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Subclass of subclass Fab C++ 0 08-09-2012 09:54 AM
can a subclass method determine if called by superclass? Peter Python 6 01-05-2012 06:59 PM
subclass a class in the namespace of the that subclass Trans Ruby 8 10-23-2008 07:24 AM
String subclass method returns subclass - bug or feature? S.Volkov Ruby 2 03-12-2006 06:46 PM
subclass has a variable that is subclass of same superclass jstorta Java 3 02-20-2006 08:42 PM



Advertisments