Polymorphic instance checking in Python

Michael Hudson mwh21 at cam.ac.uk
Wed Mar 29 06:25:29 EST 2000


lss at excosoft.se writes:

> How is polymorphic type checking accomplished in Python? For example:
> class Super: pass
> class Sub(Super): pass
> i = Sub()
> How can I perform a test on 'i' to see if it is in fact an instance of
> the Sub or Super classes (which should be true in both cases since Sub
> is a subclass of Super)? 

>>> print issubclass.__doc__; print isinstance.__doc__ 
issubclass(C, B) -> Boolean

Return whether class C is a subclass (i.e., a derived class) of class B.
isinstance(object, class-or-type) -> Boolean

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.

Cheers,
M.

-- 
well, take it from an old hand: the only reason it would be easier
to program in C is that you can't easily express complex  problems
in C, so you don't.                 -- Erik Naggum, comp.lang.lisp



More information about the Python-list mailing list