python attrs_在python中dict和attrs是什么关系?

一个类的所有属性都存储在字典中。请注意,方法也是属性;它们恰好是可调用的。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;方法是通过将类命名空间中的函数绑定到查找方法名的实例来创建的。在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值