Introspection: determining parent classes?

Jeff Epler jepler at
Fri Jun 28 16:16:16 EDT 2002

On Fri, Jun 28, 2002 at 08:07:46PM +0000, Robb Shecter wrote:
> Hi,
> Is there any way to determine the parent classes given an instance?
> I'm writing unit test code, and I want to assert that a particular 
> result is a subclass of some other class.  In Java, you can do something 
> like:
> anInstance.getClass().isSubclassOf( aClass )
> I looked through the type(), __class__, etc. info, but these seem to 
> just return string-like objects that don't allow further introspection.

They're not string-like objects.

>>> x = 1
>>> type(x)
<type 'int'>
>>> type(type(x))
<type 'type'>
>>> help(isinstance)
Help on built-in function isinstance:

    isinstance(object, class-or-type-or-tuple) -> bool
    Return whether an object is an instance of a class or of a subclass thereof.
    With a type as second argument, return whether that is the object's type.
    The form using a tuple, isinstance(x, (A, B, ...)), is a shortcut for
    isinstance(x, A) or isinstance(x, B) or ... (etc.).
>>> help(issubclass)
Help on built-in function issubclass:

    issubclass(C, B) -> bool
    Return whether class C is a subclass (i.e., a derived class) of class B.
>>> isinstance(x, type(x))
>>> isinstance(x, type(3.14159))


More information about the Python-list mailing list