The Python Language Reference, Release 3.9.5 8.7 Class definitions A class definition defines a class object (see section The standard type hierarchy ): A class definition is an executable statement. The inheritance list usually gives a list of base classes (see Metaclasses for more advanced uses), so each item in the list should evaluate to a class object which allows subclassing. Classes without an inheritance list inherit, by default, from the base class$_{object}$; hence, classdef ::= [ decorators ] "class" classname [ inheritance ] ":" suite inheritance ::= "(" [ argument_list ] ")" classname ::= identifier classFoo : pass classFoo (object): pass @f1 (arg) @f2 classFoo : pass classFoo : pass Foo =f1(arg)(f2(Foo)) 3 A string literal appearing as the first statement in the class body is transformed into the namespace’s __doc__ item and therefore the class’s docstring . Chapter 8. Compound statements 102 PEP 3115 - Metaclasses in Python 3000 The proposal that changed the declaration of metaclasses to the current syntax, and the semantics for how classes with metaclasses are constructed. is equivalent to The class’s suite is then executed in a new execution frame (see Naming and binding ), using a newly created local namespace and the original global namespace. (Usually, the suite contains mostly function definitions.) When the class’s suite finishes execution, its execution frame is discarded but its local namespace is saved. 3 A class object is then created using the inheritance list for the base classes and the saved local namespace for the attribute dictionary. The class name is bound to this class object in the original local namespace. The order in which attributes are defined in the class body is preserved in the new class’s$_{__dict__}$ . Note that this is reliable only right after the class is created and only for classes that were defined using the definition syntax. Class creation can be customized heavily using metaclasses . Classes can also be decorated: just like when decorating functions, is roughly equivalent to The evaluation rules for the decorator expressions are the same as for function decorators. The result is then bound to the class name. Changed in version 3.9: Classes may be decorated with any valid$_{assignment_expression}$ . Previously, the grammar was much more restrictive; see PEP 614 for details. Programmer’s note: Variables defined in the class definition are class attributes; they are shared by instances. Instance attributes can be set in a method with$_{self.name = value}$ . Both class and instance attributes are accessible through the notation “$_{self.name}$ ”, and an instance attribute hides a class attribute with the same name when accessed in this way. Class attributes can be used as defaults for instance attributes, but using mutable values there can lead to unexpected results. Descriptors can be used to create instance variables with different implementation details. See also: