Classes, Types, and Membership Tests

Currently, there’s a separation between types and classes. In particular, built-in types such as lists and dictionaries cannot be specialized via inheritance, nor does a class define a new type. In fact, all class definitions have a type of ClassType, while all class instances have a type of InstanceType. Thus, the following expression is true for any two objects that are instances of a class (even if they were created by different classes):

type(a) == type(b) 

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: pass class B(A): pass class C: pass ...

Get Python Essential Reference, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.