Class objects are described below. When a class object is called, a new class instance (also described below) is created and returned. This implies a call to the class’s __init__ method if it has one. Any arguments are passed on to the __init__ method. If there is no __init__ method, the class must be called without arguments.
Class instances are described below. Class instances are callable only
when the class has a __call__ method;
x(arguments) is a
Class objects are created by class definitions. A class has a namespace implemented by a dictionary object. Class attribute references are translated to lookups in this dictionary, e.g., “C.x” is translated to “C.__dict__[“x”]”. When the attribute name is not found there, the attribute search continues in the base classes. The search is depth-first, left-to-right in the order of occurrence in the base class list.
When a class attribute reference (for class C, say) would yield a user-defined function object or an unbound user-defined method object whose associated class is either C or one of its base classes, it is transformed into an unbound user-defined method object whose im_class attribute is C. When it would yield a class method object, it is transformed into a bound user-defined method object whose im_class and im_self attributes are both C. When it would yield a static method object, it is transformed into the object wrapped by the static method object. See section [3.4.2] for another way in which attributes retrieved from a class may differ from those actually contained in its __dict__.
Class attribute assignments update the class’s dictionary, never the dictionary of a base class.
A class object can be called (see above) to yield a class instance (see below).
- __name__ is the class name.
- __module__ is the module name in which the class was defined.
- __dict__ is the dictionary containing the class’s namespace.
- __bases__ is a tuple (possibly empty or a singleton) containing the base classes, in the order of their occurrence in the base class list.
- __doc__ is the class’s documentation string, or None if undefined.