descriptors Flashcards
1
Q
descriptor example
A
from weakref import WeakKeyDictionary
class Positive(): def \_\_init(self): self._instance_data = WeakKeyDictionary()
def \_\_get\_\_(self, instance, owner_class): return self._instance_data[instance]
def \_\_set\_\_(self, instance, value): if value <=0: raise ValueError() self._instance_data[instance] = value
def \_\_delete\_\_(self, instance): raise AttributeError()
2
Q
if descriptor called from a class
A
instance is None
3
Q
non-data descriptor
A
only __get__ method
4
Q
attribute lookup precedence
A
data descriptor,__dict__, non-data descriptor