__delete__(self, instance)

This method is called upon with the following statement, in which self would be the descriptor attribute, and instance would be the client object in this example:

>>> del client.descriptor

In the following example, we use this method to create a descriptor with the goal of preventing you from removing attributes from an object without the required administrative privileges. Notice how, in this case, that the descriptor has logic that is used to predicate with the values of the object that is using it, instead of different related objects:

# descriptors_methods_3.pyclass ProtectedAttribute:    def __init__(self, requires_role=None) -> None:         self.permission_required = requires_role        self._name = None def __set_name__(self, ...

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.