# 9 Classes

Python's class mechanism adds classes to the language with a minimum of new syntax and semantics. It is a mixture of the class mechanisms found in C++ and Modula-3. As is true for modules, classes in Python do not put an absolute barrier between definition and user, but rather rely on the politeness of the user not to ``break into the definition.'' The most important features of classes are retained with full power, however: the class inheritance mechanism allows multiple base classes, a derived class can override any methods of its base class(es), a method can call the method of a base class with the same name. Objects can contain an arbitrary amount of private data.

In C++ terminology, all class members (including the data members) are public, and all member functions are virtual. There are no special constructors or destructors. As in Modula-3, there are no shorthands for referencing the object's members from its methods: the method function is declared with an explicit first argument representing the object, which is provided implicitly by the call. As in Smalltalk, classes themselves are objects, albeit in the wider sense of the word: in Python, all data types are objects. This provides semantics for importing and renaming. But, just like in C++ or Modula-3, built-in types cannot be used as base classes for extension by the user. Also, like in C++ but unlike in Modula-3, most built-in operators with special syntax (arithmetic operators, subscripting etc.) can be redefined for class members.

- 9.1 A word about terminology
- 9.2 Python scopes and name spaces
- 9.3 A first look at classes
  - 9.3.1 Class definition syntax
  - 9.3.2 Class objects
  - 9.3.3 Instance objects
  - 9.3.4 Method objects
- 9.4 Random remarks
- 9.5 Inheritance
  - 9.5.1 Multiple inheritance
- 9.6 Private variables through name mangling
- 9.7 Odds and ends
  - 9.7.1 Exceptions Can Be Classes