February 2006
Intermediate to advanced
648 pages
14h 53m
English
When you create an instance of a class, the type of that instance is the class itself. To test for membership in a class, use the built-in function isinstance(obj,cname). This function returns True if an object, obj, belongs to the class cname or any class derived from cname. For example:
class A(object): pass class B(A): pass class C(object): pass a = A() # Instance of 'A' b = B() # Instance of 'B' c = C() # Instance of 'C' type(a) # Returns the class object A isinstance(a,A) # Returns True isinstance(b,A) # Returns True, B derives from A isinstance(b,C) # Returns False, C not derived from A
Similarly, the built-in function issubclass(A,B) returns True if the class A is a subclass of class B. For example:
issubclass(B,A) ...