isA function?

kosh at aesaeion.com kosh at aesaeion.com
Sat Jul 13 16:55:16 EDT 2002


On Sat, 13 Jul 2002, Roy Smith wrote:

> Is there a standard isA() function in Python?  It should take two
> arguments, an instance and a class, and return true if the object is a
> member of the class or one of the class's ancestors.
>
> I know I can roll this myself with __class__.__bases__, but it seems
> like a common enough thing that there would be a built-in way to do it.
> I looked at the inspect module, and was surprised to not find it there.
>
> We're writing some unit tests, and one of the tests is "foo() returns
> something which isA (bar)".

Look at isinstance it does what you want and takes those arguments.

isinstance(object, class)

Returns true if object is and instance of class or if class is anywhre in
object's inheritance structure.






More information about the Python-list mailing list