1. descriptor是一个对象,是一个class
2.descriptor是一个有__get__,__set__,__delete__方法的类
3.假设D是一个有上面三个方法的类,E是一个类,其中d是其的一个成员,d是D的实例,e是E的一个实例,e.d会调用D的__get__方法,get方法的参数为(self = d,obj = c,type = C) , e.d = f会调用d的__set__方法,参数一样,del e.d 会调用d的delete方法
4. 由于python寻找属性时会首先寻找类的descriptor,所以如果直接修改实例的__dict__里的desciptor,即e.__dict__['d'] = xxx,不会影响到这个descriptor
5. 有前面的描述可知,如果要去掉d这个成员,必须要通过E.d = xxx来实现,即实例本身是没有办法做到的,因此python增加了data descriptor与non-data descriptor,区别在于non-data descriptor只有__get__方法,没有另外两个方法,即只是一个数据存储的地方,python中的function就是这样的descriptor,当get function是,function将self变量插入到参数列表中然后返回一个新的函数对象,因此python的search order为:
a. python 预定义的属性
b. __class__ 里的data descriptor
c. self.__dict__
d. __class__.__dict__
e. raise attribute error
2.descriptor是一个有__get__,__set__,__delete__方法的类
3.假设D是一个有上面三个方法的类,E是一个类,其中d是其的一个成员,d是D的实例,e是E的一个实例,e.d会调用D的__get__方法,get方法的参数为(self = d,obj = c,type = C) , e.d = f会调用d的__set__方法,参数一样,del e.d 会调用d的delete方法
4. 由于python寻找属性时会首先寻找类的descriptor,所以如果直接修改实例的__dict__里的desciptor,即e.__dict__['d'] = xxx,不会影响到这个descriptor
5. 有前面的描述可知,如果要去掉d这个成员,必须要通过E.d = xxx来实现,即实例本身是没有办法做到的,因此python增加了data descriptor与non-data descriptor,区别在于non-data descriptor只有__get__方法,没有另外两个方法,即只是一个数据存储的地方,python中的function就是这样的descriptor,当get function是,function将self变量插入到参数列表中然后返回一个新的函数对象,因此python的search order为:
a. python 预定义的属性
b. __class__ 里的data descriptor
c. self.__dict__
d. __class__.__dict__
e. raise attribute error