Inheritance Queries

Alex Martelli aleaxit at
Fri Jul 13 08:21:49 EDT 2001

"Angel Asencio" <asencio at> wrote in message
news:9il5qp$iba$1 at
> Is there a package to get information about the inheritance of
> a class/instance?
> Methods or functions to answer questions like:
> Which are your decendants?
> Which are your ancestors?
> Who are your immediate decendants?
> Who are your immediate ancestors?

A class has absolutely no information about any
subclassing performed on it.  Conceptually, the
only way to gather such information at one point
in time would be to go through *all* classes
defined at that instant and check if the given
class is among the ancestors -- and I don't think
there's any sensible way to examine *all* classes
in existence at one time.

A class knows its immediate ancestors: they're
the __bases__ tuple attribute of the class object
(you get from an instance to its class via the
__class__ attribute of the instance object, of
course).  Non-immediate ancestors are obtained
through recursive walks up the inheritance DAG
(Directed Acyclic Graph).  Built-in function
issubclass() tells you, given any two classes
C1 and C2, whether C1 is a direct or indirect
subclass of C2.

Package inspect has no direct support for
inheritance information except for one function
that, given a bunch of class objects, places
them in a structure evidencing which class
inherited from which others.


More information about the Python-list mailing list