编写非数据描述符
Writing a Non-Data Descriptor
我正在学习 python 中的描述符。我想编写一个非数据描述符,但是当我调用 class 方法时,将描述符作为其 class 方法的 class 不会调用 __get__
特殊方法。这是我的例子(没有 __set__
):
class D(object):
"The Descriptor"
def __init__(self, x = 1395):
self.x = x
def __get__(self, instance, owner):
print "getting", self.x
return self.x
class C(object):
d = D()
def __init__(self, d):
self.d = d
我是这样称呼它的:
>>> c = C(4)
>>> c.d
4
描述符 class 的 __get__
没有调用。但是当我还设置 __set__
时,描述符似乎被激活了:
class D(object):
"The Descriptor"
def __init__(self, x = 1395):
self.x = x
def __get__(self, instance, owner):
print "getting", self.x
return self.x
def __set__(self, instance, value):
print "setting", self.x
self.x = value
class C(object):
d = D()
def __init__(self, d):
self.d = d
现在我创建一个 C
实例:
>>> c=C(4)
setting 1395
>>> c.d
getting 4
4
并且 __get__, __set__
都存在。似乎我缺少一些关于描述符及其使用方法的基本概念。谁能解释 __get__, __set__
的这种行为?
您成功创建了一个正确的非数据描述符,但是您随后通过设置实例属性屏蔽 d
属性。
因为它是一个非数据描述符,所以实例属性在这种情况下胜出。当你添加一个 __set__
方法时,你将你的描述符变成了一个数据描述符,即使有一个实例属性,数据描述符也总是被应用。 (*)
The default behavior for attribute access is to get, set, or delete the attribute from an object’s dictionary. For instance, a.x
has a lookup chain starting with a.__dict__['x']
, then type(a).__dict__['x']
, and continuing through the base classes of type(a)
excluding metaclasses. If the looked-up value is an object defining one of the descriptor methods, then Python may override the default behavior and invoke the descriptor method instead. Where this occurs in the precedence chain depends on which descriptor methods were defined.
和
If an object defines both __get__()
and __set__()
, it is considered a data descriptor. Descriptors that only define __get__()
are called non-data descriptors (they are typically used for methods but other uses are possible).
Data and non-data descriptors differ in how overrides are calculated with respect to entries in an instance’s dictionary. If an instance’s dictionary has an entry with the same name as a data descriptor, the data descriptor takes precedence. If an instance’s dictionary has an entry with the same name as a non-data descriptor, the dictionary entry takes precedence.
如果您删除 d
实例属性(从不设置它或从实例中删除它),将调用描述符对象:
>>> class D(object):
... def __init__(self, x = 1395):
... self.x = x
... def __get__(self, instance, owner):
... print "getting", self.x
... return self.x
...
>>> class C(object):
... d = D()
...
>>> c = C()
>>> c.d
getting 1395
1395
再次添加实例属性,描述符被忽略,因为实例属性获胜:
>>> c.d = 42 # setting an instance attribute
>>> c.d
42
>>> del c.d # deleting it again
>>> c.d
getting 1395
1395
另请参阅 Python Datamodel 参考中的 Invoking Descriptors documentation。
(*) 前提是数据描述符实现了 __get__
挂钩。通过 instance.attribute_name
访问这样的描述符将 return 描述符对象 除非 'attribute_name'
存在于 instance.__dict__
.
我正在学习 python 中的描述符。我想编写一个非数据描述符,但是当我调用 class 方法时,将描述符作为其 class 方法的 class 不会调用 __get__
特殊方法。这是我的例子(没有 __set__
):
class D(object):
"The Descriptor"
def __init__(self, x = 1395):
self.x = x
def __get__(self, instance, owner):
print "getting", self.x
return self.x
class C(object):
d = D()
def __init__(self, d):
self.d = d
我是这样称呼它的:
>>> c = C(4)
>>> c.d
4
描述符 class 的 __get__
没有调用。但是当我还设置 __set__
时,描述符似乎被激活了:
class D(object):
"The Descriptor"
def __init__(self, x = 1395):
self.x = x
def __get__(self, instance, owner):
print "getting", self.x
return self.x
def __set__(self, instance, value):
print "setting", self.x
self.x = value
class C(object):
d = D()
def __init__(self, d):
self.d = d
现在我创建一个 C
实例:
>>> c=C(4)
setting 1395
>>> c.d
getting 4
4
并且 __get__, __set__
都存在。似乎我缺少一些关于描述符及其使用方法的基本概念。谁能解释 __get__, __set__
的这种行为?
您成功创建了一个正确的非数据描述符,但是您随后通过设置实例属性屏蔽 d
属性。
因为它是一个非数据描述符,所以实例属性在这种情况下胜出。当你添加一个 __set__
方法时,你将你的描述符变成了一个数据描述符,即使有一个实例属性,数据描述符也总是被应用。 (*)
The default behavior for attribute access is to get, set, or delete the attribute from an object’s dictionary. For instance,
a.x
has a lookup chain starting witha.__dict__['x']
, thentype(a).__dict__['x']
, and continuing through the base classes oftype(a)
excluding metaclasses. If the looked-up value is an object defining one of the descriptor methods, then Python may override the default behavior and invoke the descriptor method instead. Where this occurs in the precedence chain depends on which descriptor methods were defined.
和
If an object defines both
__get__()
and__set__()
, it is considered a data descriptor. Descriptors that only define__get__()
are called non-data descriptors (they are typically used for methods but other uses are possible).Data and non-data descriptors differ in how overrides are calculated with respect to entries in an instance’s dictionary. If an instance’s dictionary has an entry with the same name as a data descriptor, the data descriptor takes precedence. If an instance’s dictionary has an entry with the same name as a non-data descriptor, the dictionary entry takes precedence.
如果您删除 d
实例属性(从不设置它或从实例中删除它),将调用描述符对象:
>>> class D(object):
... def __init__(self, x = 1395):
... self.x = x
... def __get__(self, instance, owner):
... print "getting", self.x
... return self.x
...
>>> class C(object):
... d = D()
...
>>> c = C()
>>> c.d
getting 1395
1395
再次添加实例属性,描述符被忽略,因为实例属性获胜:
>>> c.d = 42 # setting an instance attribute
>>> c.d
42
>>> del c.d # deleting it again
>>> c.d
getting 1395
1395
另请参阅 Python Datamodel 参考中的 Invoking Descriptors documentation。
(*) 前提是数据描述符实现了 __get__
挂钩。通过 instance.attribute_name
访问这样的描述符将 return 描述符对象 除非 'attribute_name'
存在于 instance.__dict__
.