testing for class of instance

Remco Gerlich scarblac at pino.selwerd.nl
Sun May 13 03:32:17 EDT 2001

Albert Wagner <alwagner at tcac.net> wrote in comp.lang.python:
> How do I test for the class of an instance?  e.g., I used to test for a 
> dictionary:
> if type(aContainer) == type({}):

   if aContainer.__class__ == MyClass:

with will be false for an instance of a class that inherits MyClass, or

   if isinstance(aContainer, MyClass):

which is also true for inherited objects. In fact, it works for types as well:

   if isinstance(aContainer, type({})):


Remco Gerlich

More information about the Python-list mailing list