笔记-python属性查找 (attribute lookup)

在Python中,属性查找(attribute lookup)是比较复杂的,特别是涉及到描述符descriptor的时候。

在上一文章末尾,给出了一段代码,就涉及到descriptor与attribute lookup的问题。而get系列函数(get, getattr, getattribute) 也很容易搞晕,本文就这些问题简单总结一下。
  首先,我们知道:

    python中一切都是对象,“everything is object”,包括类,类的实例,数字,模块

    任何object都是类(class or type)的实例(instance)

    如果一个descriptor只实现了__get__方法,我们称之为non-data descriptor, 如果同时实现了__get__ __set__我们称之为data descriptor。

实例属性查找

按照python doc,如果obj是某个类的实例,那么obj.name(以及等价的getattr(obj,‘name’))首先调用__getattribute__。如果类定义了__getattr__方法,那么在__getattribute__抛出 AttributeError 的时候就会调用到__getattr__,而对于描述符(get)的调用,则是发生在__getattribute__内部的。官网文档是这么描述的

    The implementation works through a precedence chain that gives data descriptors priority over instance variables, instance variables priority over non-data descriptors, and assigns lowest priority to __getattr__() if provided.

obj = Clz(), 那么obj.attr 顺序如下:

(1)如果“attr”是出现在Clz或其基类的__dict__中, 且attr是data descriptor, 那么调用其__get__方法, 否则

(2)如果“attr”出现在obj的__dict__中, 那么直接返回 obj.__dict__['attr'], 否则

(3)如果“attr”出现在Clz或其基类的__dict__中

    (3.1)如果attr是non-data descriptor,那么调用其__get__方法, 否则

    (3.2)返回 __dict__['attr']

(4)如果Clz有__getattr__方法,调用__getattr__方法,否则

(5)抛出AttributeError 

下面是测试代码:
  
View Code

注意第50行,change_attr给实例的__dict__里面增加了两个属性。通过上下两条print的输出如下:

  Derive object dict {'same_name_attr': 'attr in object', 'not_des_attr': 'I am not descriptor attr'}
  Derive object dict {'same_name_attr': 'attr in object', 'ndd_derive': 'ndd_derive now in object dict ', 'not_des_attr': 'I am not descriptor attr', 'dd_base': 'dd_base now in object dict '}

调用change_attr方法之后,dd_base既出现在类的__dict__(作为data descriptor), 也出现在实例的__dict__, 因为attribute lookup的循序,所以优先返回的还是Clz.dict[‘dd_base’]。而ndd_base虽然出现在类的__dict__, 但是因为是nondata descriptor,所以优先返回obj.dict[‘dd_base’]。其他:line48,line56表明了__getattr__的作用。line49表明obj.__dict__优先于Clz.dict
cached_property例子

我们再来看看上一文章的这段代码。

1 import functools, time
 2 class cached_property(object):
 3     """ A property that is only computed once per instance and then replaces
 4         itself with an ordinary attribute. Deleting the attribute resets the
 5         property. """
 6 
 7     def 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大白砌墙

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值