一个类的所有属性都存储在字典中。请注意,方法也是属性;它们恰好是可调用的。Python在很多地方使用字典;例如,模块的全局名称空间也是字典。通常,{a1}的工作是将属性访问转换为属性字典中的键查找。在
类语句的主体像函数一样执行,生成的局部名称空间(通过调用^{}生成的字典)被传递给元类来生成类对象。这就是attrs在示例代码中绑定到的内容。在The class’s suite is then executed in a new execution frame (see [Naming and binding](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. A class object is then created using the inheritance list for the base classes and the saved local namespace for the attribute dictionary.
大胆强调我的。在Once the class namespace has been populated by executing the class body, the class object is created by calling metaclass(name, bases, namespace, **kwds)
命名空间字典随后由type.__new__()附加到新创建的类对象。在A class has a namespace implemented by a dictionary object. Class attribute references are translated to lookups in this dictionary, e.g., C.x is translated to C.__dict__["x"] (although there are a number of hooks which allow for other means of locating attributes).
您可能还需要阅读descriptors;方法是通过将类命名空间中的函数绑定到查找方法名的实例来创建的。在