August 2018
Intermediate to advanced
332 pages
9h 12m
English
We will start with a descriptor that only implements the __get__ method, and see how it is used:
class NonDataDescriptor: def __get__(self, instance, owner): if instance is None: return self return 42class ClientClass: descriptor = NonDataDescriptor()
As usual, if we ask for the descriptor, we get the result of its __get__ method:
>>> client = ClientClass()>>> client.descriptor42
But if we change the descriptor attribute to something else, we lose access to this value, and get what was assigned to it instead:
>>> client.descriptor = 43>>> client.descriptor43
Now, if we delete the descriptor, and ask for it again, let's see what we get:
>>> del client.descriptor>>> client.descriptor42
Let's rewind what just happened. When ...