Discovering Descriptors

This talk was presented at PyCon CZ 2017 and EuroPython 2017. Here are the references and resources for the presentation.

The code used for the talk, is available here and contains some explanations along with the examples.

Slides

Video

This is the recording of the talk presented at EuroPython 2017.

{{ youtube(id=“TAuC086NNmo”) }}

References

To learn more about descriptors, you could check some of the following references, used as base for the talk:

Read more...

The __set_name__ method for descriptors

Descriptors generally have to interact with attributes of the managed object, and this is done by inspecting __dict__ on that object (or calling getattr/setattr, but the problem is the same), and finding the key under the specific name.

For this reason, the descriptor will have to know the name of the key to look for, which is related to the name of the attribute is managing.

On previous versions of Python this had to be done explicitly. If we wanted to work around it, there were some more advanced ways to do so. Luckily, after PEP-487 (added in Python 3.6), there are some enhancements regarding class creation, which also affects descriptors.

Read more...

Descriptors & Decorators

Descriptors are an amazing tool to have in our toolbox, as they come in handy in many opportunities.

Probably the best thing about descriptors, is that they can improve other solutions. Let's see how we can write better decorators, by using descriptors.

Decorate a class method

Imagine we have a very simple decorator, that does nothing but returning a text, with what the original function returns:

class decorator:
    def __init__(self, func):
        self.func = func
        wraps(func)(self)

    def __call__(self, *args, **kwargs):
        result = self.func(*args, **kwargs)
        return f"decorated {result}"

class Object:
    @decorator
    @classmethod
    def class_method(cls):
        return 'class method'

If we apply the decorator to a simple function, it'll work, as expected. However, when it's applied to a class method, we can see an error:

Read more...

Types of Descriptors

Resuming from where we left off, on the previous post, on which we took a-first-look-at-descriptors{.interpreted-text role=“doc”}, it’s time to explore their different types and how they work internally.

In Python, almost everything is represented with a dictionary. Objects are dictionaries. Classes are objects, hence they also are contained into a dictionary. This is denoted by the __dict__ attribute that objects have.

There are two types of descriptors: data descriptors and non-data ones. If a descriptor implements both1 __get__() and __set__(), it's called a data descriptor; otherwise is a non-data descriptor.

Read more...

A first look at descriptors

Descriptors are one of the most powerful features of Python. The reason why they're so powerful is because they enable us to control the core operations (get, set, delete)1, of an attribute in a given object, so that we can hook a particular code, controlled by us, in order to modify, change, or extend the original operation.

A descriptor is an object that implements either __get__, __set__, or __delete__.

As of Python 3.6+2 the descriptor protocol entails these methods:

Read more...