50
338
Object-Oriented Programming: Inheritance
Chapter 9
• There are cases in which it is useful to define classes for which the programmer never intends to
create any objects. Such classes are called abstract classes.
• The sole purpose of an abstract class is to provide an appropriate base class from which classes
may inherit interface and possibly implementation. Classes from which objects can be created are
called concrete classes.
• Python does not provide a way to designate an abstract class. However, the programmer can im-
plement an abstract class by raising an exception in the class’s __init__ method.
• Python is inherently polymorphic because the language is dynamically typed. This means that Py-
thon determines at runtime whether an object defines a method or contains an attribute and, if so,
calls the appropriate method or accesses the appropriate attribute.
• Using polymorphism, one method call can cause different actions to occur depending on the class
of the object receiving the call. This gives the programmer tremendous expressive capability.
• Beginning with Python 2.2, the nature and behavior of classes will change. In all future 2.x releas-
es, a programmer can distinguish between two kinds of classes: “classic” classes and “new” class-
es. In Python 3.0, all classes will behave like “new” classes.
• Python 2.2 provides type object for defining “new” classes. Any class that inherits from ob-
ject exhibits the new-class behaviors.
• “New” classes can define static methods. A static method can be called by a client of the class,
even if no objects of the class exist.
• A class designates a method as static by passing the method’s name to built-in function static-
method and binding a name to the value returned from the function call.
• Static methods differ from regular methods in that when a program calls a static method, Python
does not pass the object reference argument to the method. Therefore, a static method does not
specify self as the first argument.
• The goal of the new class behavior is to remove the dichotomy that existed between Python types
and classes before version 2.2. The most practical use of this type-class unification is that program-
mers now can inherit from Python’s built-in types.
• Classes that inherit from base-class object also can define method __getattribute__,
which executes for every attribute access.
• Method __getattribute__ in a derived class must call the base-class version of the method
to retrieve an object’s attribute; otherwise, infinite recursion occurs.
• Python 2.2 allows “new” classes to define a __slots__ attribute listing the attributes that ob-
jects of the class are allowed to have.
• When a “new” class defines the __slots__ attribute, objects of the class can assign values only
to attributes whose names appear in the __slots__ list. If a client attempts to assign a value to
an attribute whose name does not appear in __slots__, Python raises an exception.
• ““New” classes can contain properties that describe object attributes. A program accesses an ob-
ject’s properties in the same manner as accessing the object’s attributes.
• A class definition creates a property by specifying four components—a get method, a set method,
a delete method and a docstring that describes the property. The get, set and delete methods can
perform any tasks necessary for maintaining data in a consistent state.
• Classes that use properties most often define their attributes to be private, to hide the data from
clients of the class. The clients of the class then access the public properties of that class, which
get and set the values of the private attributes.
• Built-in function property takes as arguments a get method, a set method, a delete method and
a docstring and returns a property for the class.
pythonhtp1_09.fm Page 338 Friday, December 14, 2001 2:01 PM