Classes are Python’s main object-oriented programming (OOP) tool. They support multiple instances, attribute inheritance, and operator overloading.
Inheritance happens at attribute qualification time: on object.attribute
, if object
is a class or instance.
Classes inherit attributes from all classes listed in their class statement header line (superclasses). Listing more than one means multiple inheritance.
Instances inherit attributes from the class from which they are generated, plus all that class’s superclasses.
Inheritance searches the instance, then its class, then all accessible superclasses, and uses the first version of an attribute name found. Superclasses are searched depth-first and then left-to-right (but new-style classes search across before proceeding up in diamond pattern trees).
By default, all attribute names in modules and classes are visible everywhere. Special conventions allow some limited data-hiding but are mostly designed to prevent name collisions (see also the section Name conventions).
Names in modules with a single underscore (e.g., _X
), and those not listed on the module’s
__all__
list, are not copied over
when a client uses from module import
*
. This is not strict privacy, though, as such names can
still be accessed with other import statement forms.
Names anywhere within class
statements with two leading
underscores only (e.g., __X
) are
mangled at compile time to include the enclosing class name as a
prefix (e.g., _Class__X
). The
added class-name prefix localizes such names to the enclosing class
and thus makes them distinct in both the self
instance object and the class
hierarchy.
This helps to avoid clashes that may arise for same-named
methods, and for attributes in the single instance object at the
bottom of the inheritance chain (all assignments to self.attr
anywhere in a framework change
the single instance namespace). This is not strict privacy, though,
as such attributes can still be accessed via the mangled
name.
In Python 3.0, there is a single class model: all classes are considered
new-style whether they derive from object
or not. In Python 2.X, there are two
class models: classic (the default), and
new-style in version 2.2 and later (coded by
deriving from a built-in type or object
—class
A(object)
).
New-style classes (and all classes in Python 3.0) differ from classic classes in the following ways:
Diamond patterns of multiple inheritances have a slightly different search order—roughly, they are searched across before up, and more breadth-first than depth-first.
Classes are now types, and types are now classes. The
type(I)
built-in returns the
class an instance is made from, instead of a generic instance
type, and is normally the same as I.__class__
. The type
class may be subclassed to
customize class creation, and all classes inherit from object
.
The __getattr__
and
__getattribute__
methods are no
longer run for attributes implicitly fetched by built-in
operations. They are not called for __
X
__
operator
-overloading method names; the search for such names
begins at classes, not instances. To intercept and delegate access
to such method names, they generally must be redefined in
wrapper/proxy classes.
New-style classes have a set of new class tools, including
slots, properties, descriptors, and the __getattribute__
method. Most of these
have tool-building purposes. See
the next section for __slots__
, __getattribute__
, and descriptor
__get__
, __set__
, and __delete__
methods; see Built-in Functions for property()
.