为什么在描述符 class 的 __get__ 中需要 "if instance is None"?
why do you need "if instance is None" in __get__ of a descriptor class?
我从 Effective Python 项目 31 中得到以下示例:
from weakref import WeakKeyDictionary
class Grade(object):
def __init__(self):
self._values = WeakKeyDictionary()
def __get__(self, instance, instance_type):
if instance is None: return self
return self._values.get(instance, 0)
def __set__(self, instance, value):
if not (0 <= value <= 100):
raise ValueError('Grade must be between 0 and 100')
self._values[instance] = value
# Example 16
class Exam(object):
math_grade = Grade()
writing_grade = Grade()
science_grade = Grade()
first_exam = Exam()
first_exam.writing_grade = 82
second_exam = Exam()
second_exam.writing_grade = 75
print('First ', first_exam.writing_grade, 'is right')
print('Second', second_exam.writing_grade, 'is right')
我想不出任何理由让 if instance is None: return self
出现在 __get__
中。 Exam
(或其他使用 Grade
的潜在 类)实例如何成为 None
?
Python 将在访问 class.
上的描述符时为实例传入 None
在这种情况下,通过返回 self
,您可以访问 class 上的描述符对象,而无需绕过协议(通过访问 ClassObj.__dict__['name_of_descriptor']
)。
>>> class DemoDescriptor:
... def __get__(self, instance, type_):
... if instance is None:
... print('Accessing descriptor on the class')
... return self
... print('Accessing descriptor on the instance')
... return 'Descriptor value for instance {!r}'.format(instance)
...
>>> class DemoClass(object):
... foo = DemoDescriptor()
...
>>> DemoClass.foo # on the class
Accessing descriptor on the class
<__main__.DemoDescriptor object at 0x1041d3c50>
>>> DemoClass.__dict__['foo'] # bypassing the descriptor protocol
<__main__.DemoDescriptor object at 0x1041d3c50>
>>> DemoClass().foo # on the instance
Accessing descriptor on the instance
'Descriptor value for instance <__main__.DemoClass object at 0x1041d3438>'
这也是 function
和 property
对象的 __get__
方法实现的工作方式。
对于您的具体情况,Exam.math_grade
、Exam.writing_grade
或 Exam.science_grade
中的每一个都将调用 Grade.__get__
,为实例传入 None
,并且 Exam
为 instance_type
.
我从 Effective Python 项目 31 中得到以下示例:
from weakref import WeakKeyDictionary
class Grade(object):
def __init__(self):
self._values = WeakKeyDictionary()
def __get__(self, instance, instance_type):
if instance is None: return self
return self._values.get(instance, 0)
def __set__(self, instance, value):
if not (0 <= value <= 100):
raise ValueError('Grade must be between 0 and 100')
self._values[instance] = value
# Example 16
class Exam(object):
math_grade = Grade()
writing_grade = Grade()
science_grade = Grade()
first_exam = Exam()
first_exam.writing_grade = 82
second_exam = Exam()
second_exam.writing_grade = 75
print('First ', first_exam.writing_grade, 'is right')
print('Second', second_exam.writing_grade, 'is right')
我想不出任何理由让 if instance is None: return self
出现在 __get__
中。 Exam
(或其他使用 Grade
的潜在 类)实例如何成为 None
?
Python 将在访问 class.
上的描述符时为实例传入None
在这种情况下,通过返回 self
,您可以访问 class 上的描述符对象,而无需绕过协议(通过访问 ClassObj.__dict__['name_of_descriptor']
)。
>>> class DemoDescriptor:
... def __get__(self, instance, type_):
... if instance is None:
... print('Accessing descriptor on the class')
... return self
... print('Accessing descriptor on the instance')
... return 'Descriptor value for instance {!r}'.format(instance)
...
>>> class DemoClass(object):
... foo = DemoDescriptor()
...
>>> DemoClass.foo # on the class
Accessing descriptor on the class
<__main__.DemoDescriptor object at 0x1041d3c50>
>>> DemoClass.__dict__['foo'] # bypassing the descriptor protocol
<__main__.DemoDescriptor object at 0x1041d3c50>
>>> DemoClass().foo # on the instance
Accessing descriptor on the instance
'Descriptor value for instance <__main__.DemoClass object at 0x1041d3438>'
这也是 function
和 property
对象的 __get__
方法实现的工作方式。
对于您的具体情况,Exam.math_grade
、Exam.writing_grade
或 Exam.science_grade
中的每一个都将调用 Grade.__get__
,为实例传入 None
,并且 Exam
为 instance_type
.