Data descriptors

Now, let's look at the difference of using a data descriptor. For this, we are going to create another simple descriptor that implements the __set__ method:

class DataDescriptor:    def __get__(self, instance, owner):        if instance is None:            return self        return 42    def __set__(self, instance, value):        logger.debug("setting %s.descriptor to %s", instance, value)        instance.__dict__["descriptor"] = valueclass ClientClass:    descriptor = DataDescriptor()

Let's see what the value of the descriptor returns:

>>> client = ClientClass()>>> client.descriptor42

Now, let's try to change this value to something else, and see what it returns instead:

>>> client.descriptor = 99>>> client.descriptor42

The value returned by the descriptor didn't ...

Get Clean Code in Python now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.