Object-Oriented Programming

Classes are Python’s main object-oriented programming (OOP) tool. They support multiple instances, attribute inheritance, and operator overloading.

Classes and Instances

Class objects provide default behavior

  • The class statement creates a class object and assigns it to a name.

  • Assignments inside class statements create class attributes, which are inherited object state and behavior.

  • Class methods are nested defs, with special first arguments to receive the implied subject instance.

Instance objects are generated from classes

  • Calling a class object like a function makes a new instance object.

  • Each instance object inherits class attributes and gets its own attribute namespace.

  • Assignments to attributes of the first argument (e.g., self.X = V) in methods create per-instance attributes.

Inheritance rules

  • 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).

Pseudoprivate Attributes

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).

Module privates

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.

Class privates

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.

New Style Classes

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 objectclass 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().

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset