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()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

if descriptor called from a class

A

instance is None

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

non-data descriptor

A

only __get__ method

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

attribute lookup precedence

A

data descriptor,__dict__, non-data descriptor

How well did you know this?
1
Not at all
2
3
4
5
Perfectly