Creating a class
The core of the object-oriented program is the class definition. The class statement creates an object that is used to create instances of the class. When we create a new class, SomeClass
, we can then use that SomeClass()
function to create objects that share the common definitions of the class. This is the way the built-in classes all work; for example, the int()
function creates an instance of the int
class.
In Python, a class
statement includes the method functions that describe the behavior of each instance. In addition to ordinary methods, there are several varieties of "special" methods which are intimately bound to the way Python operates.
We aren't obligated—in any formal way—to provide specific attributes (also called instance variables) for a class. The instance variables of an object are flexible, and are not defined in advance.
The initial clause of a class
statement provides the class name. It can also name any superclasses, from which features are inherited. The...