A class definition defines a class object (see section The standard type hierarchy): classdef ::= [decorators] "class" classname [inheritance] ":" suite inheritance ::= "(" [argument_list] ")" classname ::= identifier 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, class Foo: pass is equivalent to class Foo(object): pass 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, @f1(arg) @f2 class Foo: pass is roughly equivalent to class Foo: pass Foo = f1(arg)(f2(Foo)) The evaluation rules for the decorator expressions are the same as for function decorators. The result is then bound to the class name. **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: **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. **PEP 3129** - Class Decorators The proposal that added class decorators. Function and method decorators were introduced in **PEP 318**. Related help topics: CLASSES, SPECIALMETHODS
类定义定义了一个类对象(参见标准部分) 类型层次结构): classdef::= [decorator] "class" classname[继承]":" suite 继承::= "(" [argument_list] ")" 类名::=标识符 类定义是一个可执行语句。继承列表 通常给出基类的列表(更多信息请参阅元类) 所以列表中的每一项都应该被评估为一个类 对象,该对象允许子类化。没有继承列表的类 默认情况下继承基类“object”;因此, 类Foo: 通过 相当于 类Foo(对象): 通过 然后类的套件在一个新的执行框架中执行(参见 ),使用新创建的本地名称空间和 原来的全局名称空间。(通常,套件包含了大部分 函数定义)。当类的套件完成执行时,它的 执行框架被丢弃,但其本地名称空间被保存。[3]一个 然后使用基类的继承列表创建类对象 类和保存的属性字典的本地名称空间。 类名在原始本地绑定到这个类对象 名称空间。 类主体中定义属性的顺序为 保留在新类别的“剩余书写__”中。注意,这是可靠的 仅在类创建之后,且仅适用于已创建的类 使用定义语法定义。 可以使用元类大量定制类的创建。 类也可以被装饰:就像装饰函数一样, @f1 (arg) @f2 类Foo:通过 大致相当于 类Foo:通过 Foo = f1 (arg) (f2 (Foo)) 装饰器表达式的计算规则与for相同 函数修饰符。然后将结果绑定到类名。 **程序员注意:**在类定义中定义的变量是 类属性;它们由实例共享。实例属性 可以在方法中设置"self。name = value"。类和 实例属性可以通过“self。name”符号访问, 实例属性隐藏具有相同名称的类属性 以这种方式访问时。类属性可以作为默认值使用 但是使用可变值可能导致 意想不到的结果。描述符可用于创建实例 具有不同实现细节的变量。 参见: **PEP 3115** - Python 3000中的元类 将元类的声明更改为 当前的语法,和如何类的语义 元类的构造。 **PEP 3129** -类装饰器 添加类装饰器的建议。函数和方法 decorator是在**PEP 318**中引入的。 相关帮助主题:类、特殊方法
原文:https://www.cnblogs.com/Teyisang/p/13462864.html