Introspection: determining parent classes?
jepler at unpythonic.net
Fri Jun 28 22:16:16 CEST 2002
On Fri, Jun 28, 2002 at 08:07:46PM +0000, Robb Shecter wrote:
> 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
> 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
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 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