Arg. Silly question!
Christian Tanzer
tanzer at swing.co.at
Sat May 6 02:49:40 EDT 2000
Courageous <jkraska1 at san.rr.com> wrote :
> Okay, in all the documentation, I can't find the way that
> one goes about determining an object's type/ancestry.
>
> I have two different types of things that I want to do:
>
> 1. Figure out if an object is inherited from a particular
> class.
>
> 3. Figure out if an object is an instance of a particular
> class.
Look for `isinstance' and `issubclass' in the Python Library
Reference.
`isinstance (o, c)' tells you if `o' is an instance of class `c'.
`issubclass (c, p)' tells you if class `c' is a (direct or indirect)
subclass of class `p'.
For instance:
-------------------------------------------------------------------------------
Python 1.5.2 (#5, Jan 4 2000, 11:37:02) [GCC 2.7.2.1] on linux2
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> class A : pass
...
>>> class B (A) : pass
...
>>> class C (B) : pass
...
>>> a = A()
>>> b = B()
>>> c = C()
>>> isinstance (a,A)
1
>>> isinstance (a,B)
0
>>> isinstance (b,B)
1
>>> isinstance (b,A)
1
>>> issubclass (A,A)
1
>>> issubclass (A,B)
0
>>> issubclass (B,A)
1
>>> issubclass (C,A)
1
>>> issubclass (C,B)
1
-------------------------------------------------------------------------------
--
Christian Tanzer tanzer at swing.co.at
Glasauergasse 32 Tel: +43 1 876 62 36
A-1130 Vienna, Austria Fax: +43 1 877 66 92
More information about the Python-list
mailing list