Search the courses, subjects, tracks, games and features, or jump straight to a page.
Learn it your way
A short class with Sarah and her students. The other ways are under More.
Sign up free to listen. Transcripts are free to read.
Today, we’re going to learn about descriptors in Python. Can anyone tell me what a descriptor is?
Is it related to how we manage attributes in classes?
Exactly! Descriptors provide a means to customize attribute access in classes through methods like get, set, and delete. These methods control how attributes behave.
So, they allow us to add additional functionality without directly modifying the attributes?
Yes, exactly. Think of descriptors as a way to validate or transform data on attribute access. For example, if we wanted to ensure an attribute is always a specific type, descriptors are perfect for that!
Can you give us an example?
Sure! Let's look at the Typed descriptor example that follows, where we ensure that certain attributes conform to specified data types.
Let’s create a Typed descriptor that checks the type of an attribute. Here’s the basic structure: it takes a name and an expected type.
How does it check the type when assigning a value?
Good question! In the __set__ method of our Typed descriptor, we raise a TypeError if the provided value doesn’t match the expected type.
What happens if someone tries to delete the attribute?
That’s handled in the __delete__ method where we prevent deletion by raising an AttributeError. This way, we ensure that critical attributes remain intact.
Now, let’s see how to use our Typed descriptor in a class like Person.
What are the attributes we’re going to validate?
We will validate name to be a string and age to be an integer. So, if we try to assign a string to age, it should raise an error.
Can we test that out?
Sure! When we assign a wrong type to age, like a string, it raises a TypeError. Let’s implement and test this now.
Great job today, everyone! So what have we learned about custom descriptors?
We learned to create a Typed descriptor that validates attribute types!
And we saw how to prevent deletions of important attributes!
Absolutely! Remember, by using descriptors, we can maintain data integrity and make our classes robust. Next, try implementing your own descriptor as homework.
If you set a type that's wrong, a TypeError won't be long!
Imagine you have a robot that only accepts specific parts. If you try to give it the wrong part, it won't work. This is like our Typed descriptor validating types!
Remember GSD: Get, Set, Delete for descriptor methods.
DPT - Descriptor, Proxy, Type. Descriptors proxy access to manage attribute types.
Term
What is a descriptor?
Click to flip
Term
What does the __set__ method do?
Click to flip
Term
What is the purpose of the Typed descriptor?
Click to flip
Term
What happens if an incorrect type is assigned?
Click to flip
An object attribute with binding behavior that defines methods controlling how attributes are accessed.
A custom descriptor that validates the type of assigned values to class attributes.
A method in the descriptor protocol that retrieves the value of an attribute.
A method in the descriptor protocol that sets the value of an attribute, with type checking.
A method in the descriptor protocol that deletes an attribute, often restricting deletion.
This section introduces custom descriptors in Python, enabling enhanced control over attribute management through type validation.
In this section, readers will learn how to create custom descriptors that validate attribute types in Python classes. The Typed descriptor example illustrates how to enforce specific data types for attributes like 'name' and 'age'.
In this section, we explore how to create custom descriptors in Python, specifically focusing on type validation for class attributes. Descriptors allow you to define methods that control how attributes are accessed and modified, providing a flexible mechanism for data validation.
Typed descriptor which validates that assigned values conform to a specified type.__get__, __set__, and __delete__ methods of the descriptor protocol allow for fine-grained control over how attributes behave when accessed or modified.Person class uses the Typed descriptor to ensure that the name is always a string and age is always an integer. If incorrect types are assigned, a TypeError is raised.Using such descriptors makes it easy to enforce constraints and ensure data integrity within our classes.