How do I check if an object is an instance of a given class or of a subclass of it?
Use the built-in function isinstance.
if isinstance(obj, MyClass): print "obj is my object"
You can check if an object is an instance of any of a number of classes by providing a tuple instead of a single class, e.g. isinstance(obj, (class1, class2, …)), and can also check whether an object is one of Python’s built-in types. Examples:
if isinstance(obj, str): print repr(obj), "is an 8-bit string" if isinstance(obj, basestring): print repr(obj), "is some kind of string" if