Name

The class Statement

Synopsis

class name [( super [, super]* )]:
    suite

Makes new class objects, which are factories for instance objects. Builds a new class object and assigns it to variable name. The class statement introduces a new local name scope, and all names assigned in the class statement generate class object attributes shared by all instances of the class. Important class features include the following (see also Section 1.10 and Section 1.11):

  • Superclasses from which the new class inherits attributes are listed in parentheses in the header (e.g., class Sub(Super1, Super2):).

  • Assignments in the suite generate class attributes inherited by instances: nested def statements make methods, assignment statements make simple class members, etc.

  • Calling the class generates instance objects. Each instance object has its own attributes and inherits the attributes of the class and all of its superclasses.

  • Specially named method definitions overload operations.

Get Python Pocket Reference, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.