Loading video player…

Introducing Descriptors

00:00 In this lesson, you’ll go through a brief introduction to descriptors, and a good place to start would be to understand what the point of them is actually.

00:09 So the descriptor purpose is to create your own magic. Yes, indeed. And when it comes to descriptors, that means that you decide what happens when class attributes are accessed using the dot notation.

00:24 So later in this course, you’ll see an example where you can add functionality such that when a class instance is created, the input parameters are automatically validated.

00:35 And that is, of course, just an example. You can create whatever magic you like. Now, before you can start coding though, you’ll need to study the definition first.

00:46 So according to the official documentation, this is the definition. In general, a descriptor is an attribute value that has one of the methods in the descriptor protocol.

00:59 That raises two questions for me. One, what’s a descriptor protocol? And secondly, how can an attribute value have methods?

01:08 I’m aware of classes having methods, so if my attribute value is a class or a class instance, it can have methods.

01:18 That might sound a little bit confusing at the moment, but don’t worry. At the end of the lesson, there’s actually a clear example of what that exactly means.

01:27 But for now though, let me talk you through the descriptor protocol. And for that, I’ll start with the question, What is a protocol? Well, that is a set of methods or attributes that an object must have to be considered of a given type, and that is probably also not entirely clear just based on what you’re seeing on the screen.

01:48 So I’ll include a link to a great Real Python tutorial, but secondly, let me make that a little bit clearer by looking at the descriptor protocol specifically.

02:00 So for the descriptor protocol, which are those methods or attributes that define that descriptor protocol, and what type of object must use these methods?

02:12 Firstly, looking at the methods. Well, these are the methods that define the descriptive protocol. __get__ or dunder get, __ set__, __delete__, and then there’s a fourth one, __set_name__. That one’s actually optional.

02:28 That’s a lot of underscores on one slide. So I’ll also include a link to a video tutorial that explains the use of underscores in Python. These methods seem to have input parameters like self and obj, which I imagine is object and object type, and value and owner and name. For now, don’t worry about that.

02:50 We will look at what exactly those simple parameters are when we look at the code.

02:56 So those are the methods. Now, what is the type of object that must have those methods? Well, of course that is going to be a class. And that class, or a class that has those methods, will be called a descriptor class, is also said that that class implements the descriptor protocol.

03:18 Remember that the whole point of using descriptors is to create your own magic, and this magic is the result of descriptor classes having special behavior.

03:29 Now, that is the whole point. Now, how do you trigger this special behavior of these descriptor classes?

03:36 Well, this is how it works. Firstly, you need a descriptor class. I’ve called that MyDescriptor. You need another class, which I’ve called MyClass and this other class, MyClass, needs to have a class attribute, which I have called .class_attribute.

03:52 And then the next bullet is absolutely crucial. To this class attribute, you need to assign an instance of the descriptor class. So that’s what that line of code does.

04:03 class_attribute = MyDescriptor(). So that is the instance of a descriptor class.

04:12 And once your code is set up that way, then when you access the class attribute using the dot notation, that’s when the magic that you have coded in the get and set and delete and set_name dunder methods, that’s when that magic happens.

04:29 As a final point for this lesson, I would like to come back to the descriptor definition, which was that a descriptor is an attribute value that has one of the methods in the descriptor protocol.

04:42 And this is a line of code that should make that very clear because the attribute value here is MyDescriptor() with the parentheses, which is an instance of a descriptor class.

04:53 And a descriptor class by definition has one or more of the methods of the descriptor protocol implemented.

05:03 Okay, well so much for the theory. I think the best way to learn is by doing. So in the next section, you’ll start implementing your own descriptor.

Become a Member to join the conversation.