Practical introduction to descriptors

EuroPython 2023


Abstract

Descriptors are not black magic and this practical tutorial will show you that. In fact, you use descriptors every day and you don't even know it!

Through a series of practical, hands-on exercises, you will learn

  • how to create a descriptor;
  • how to use the dunder methods __get__, __set__, and __set_name__;
  • what the descriptor protocol is; and
  • where descriptors show up in day-to-day Python code.

Description

The expected Python level for participants is “intermediate”. In practice, if you are comfortable with using the built-in property and if you have used dunder methods before, you should be fine.

This tutorial is based off of an article I wrote about descriptors.

All materials can be accessed from this GH repo.

Notes

This impromptu tutorial gave a practical introduction to descriptors.

It had not been scheduled in advance. The day before, I noticed there was an empty slot in the schedule; after talking to the organisers, we concluded it was OK for me to use that slot, so I whipped up a short, practical tutorial for descriptors.

Feedback

A participant, Sofia, wrote:

"Fantastic workshop about descriptors! I came out of it having learnt a lot about what goes on in the background. Really loved the friendly approach and the practical exercises with solutions so that everyone can follow."