Descriptors
1. Descriptors
In this lesson, we'll explore descriptors, and how they can be used when building classes.2. Changing interactions with attributes
Sometimes, we'll want to control the behavior of an object's attributes. This Student class has two attributes; name, and ssn, short for social security number. Social security numbers are sensitive values that should not be directly visible, and must follow a certain format. Right now, we have very little control over how ssn is retrieved, set, or deleted. To help with this, we can use descriptors.3. Creating descriptors with @property
Descriptors are tools used to regulate the behavior of an attribute. Using descriptors, the way an attribute is retrieved, set, or deleted can be modified and controlled. There are three common ways to create Python descriptors. In this course, we'll use the property decorator, the industry-standard for creating descriptors. The property function, along with classes implementing get, set, and delete magic methods are two viable, but less popular techniques used to create descriptors. Here, we've recreated the Student class from before, but this time, we've built a descriptor for the ssn attribute. This descriptor is made up of three decorated "getter", "setter", and "deleter" methods which take the name of the attribute they are regulating. These methods will interact indirectly with the self-dot-ssn attribute, without needing to refer to it within the method bodies. Let's take a little closer look at each of these methods.4. @property
We start by defining the first method of our ssn descriptor, called ssn, with the property decorator. This method regulates the logic executed when the ssn attribute is retrieved, and is known as the "getter" method of a descriptor. Here, we've specified that only the last four digits of the ssn attribute should be returned, with the rest of the digits redacted using X's. In this method, as well as the "setter" and "deleter", we'll interact with self-dot-_ssn, rather than self-dot-ssn directly. Behind the scenes, the property decorator manages this relationship between self-dot-ssn and self-dot-_ssn. This is an important convention when building descriptors.5. @ssn.setter
The "setter" method of our descriptor is again named ssn, and takes an updated attribute value. The syntax attribute-name-dot-setter must be used when decorating the "setter" method of a descriptor. Here, that means our decorator will be at-ssn-dot-setter. It's common to add logic to validate data quality or perform operations on other attributes in this method. Like before, we'll use use self-dot-_ssn to store the new value for ssn in this method, rather than self-dot-ssn.6. @ssn.deleter
The final method of our descriptor is the "deleter". This method is decorated with ssn-dot-deleter, and is executed when attempting to delete the ssn attribute. In our case, this method raises an AttributeError, rather than allowing the ssn attribute to be deleted.7. Descriptors in action
Now, when trying to access the ssn attribute, only the last four digits of ssn are output, prefixed with X's. A new value can be stored using the ssn attribute with the help of dot-notation, and output in the same format as before. Our "deleter" method raises an AttributeError when attempting to delete ssn, which we can see here.8. Let's practice!
Awesome work - now, it's time to practice!Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.