Newbie question: how to determine "is-a" relationship

Dave Brueck dbrueck at edgix.com
Thu Apr 19 12:14:10 EDT 2001


Hi Jake,

Try the issubclass and isinstance functions:

>>> class A:pass
>>> class B(A):pass
>>> class C(A):pass
>>> q = [A(),B(),C()]

>>> for z in q:
...  print z.__class__
...  print isinstance(z,A), isinstance(z,B),isinstance(z,C)

__main__.A
1 0 0
__main__.B
1 1 0
__main__.C
1 0 1
>>> print issubclass(B,A)
1
>>> print issubclass(A,A)
1
>>> print issubclass(C,A)
1
>>> print issubclass(C,B)
0

-Dave

----- Original Message -----
From: "Jake Baker" <jbaker at ummelec.com>
To: <python-list at python.org>
Sent: Thursday, April 19, 2001 9:46 AM
Subject: Newbie question: how to determine "is-a" relationship


Gosh. I've been using Python since 96 and I don't know this! Wow!

Say I have a class heirarchy that looks like A, B <- A, C <- A, etc....

I have a list of objects (which I know arbitrarily are all A or sublasses of
A) and want to filter them based on which subclass they are.

How do I go about doing this? You help is greatly appreciated!

Thanks,
 - Jake Baker


--
http://mail.python.org/mailman/listinfo/python-list





More information about the Python-list mailing list