Using Properties Instead of Getters and Setters
00:00 Using Properties Instead of Getters and Setters. The Pythonic way to attach behavior to an attribute is to turn the attribute itself into a property. Properties pack together methods for getting, setting, deleting, and documenting the underlying data. Therefore, properties are special attributes with additional behavior.
00:22 You can use properties in the same way that you use regular attributes. When you access a property, its attached getter method is automatically called. Likewise, when you mutate the property, its setter method gets called.
00:36
This behavior provides the means to attach functionality to your attributes without introducing breaking changes in your code’s API. As an example of how properties can help you attach behavior to attributes, let’s say you need an Employee
class as part of an employee management system.
00:54 You start with this bare-bones implementation.
01:03
This class constructor takes two arguments, the name and date of birth of the employee at hand. These attributes are directly stored in two instance attributes, .name
and .birth_date
.
01:18
You can start using the class right away. Employee
allows you to create instances that give you direct access to the associated name and birth date.
01:38 Note that you can also mutate the attributes using direct assignments.
01:49
As your project evolves, you have new requirements. You need to store the employee’s name in uppercase letters and turn the birth date into a date
object.
02:00 To meet these requirements without breaking your API with getter and setter methods, you can use properties.
02:24
In this enhanced version of Employee
, you turn .name
and .birth_date
into properties using the @property
decorator.
02:32
Now, each attribute has a getter and a setter method named after the attribute itself. Note that the setter of .name
turns the input name into uppercase letters.
02:51
The setter of .birth_date
automatically converts the input date into a date
object for you.
03:06 As mentioned previously, a neat feature of properties is that you can use them as regular attributes.
03:35
You’ve added behavior to the .name
and .birth_date
attributes without affecting your class’s API. With properties, you’ve gained the ability to refer to these attributes as you would to regular attributes. Behind the scenes, Python takes care of running the appropriate methods for you.
03:54
You must avoid breaking your user’s code by introducing changes into your APIs. Python’s @property
decorator is the Pythonic way to do that.
04:03 Properties are officially recommended in PEP 8 as the right way to deal with attributes that need functional behavior. Python’s properties have a lot of potential use cases. For example, you can use properties to create read-only, read-write, and write-only attributes in an elegant and straightforward manner.
04:24 Properties allow you to delete and document the underlying attributes and more. More importantly, properties allow you to make regular attributes behave like manage attributes with attached behavior without changing the way that you work with them. Because of properties, Python developers tend to design their classes’ APIs using a few guidelines.
04:45 Use public attributes whenever appropriate, even if you expect the attribute to require functional behavior in the future. Avoid defining getter and setter methods for your attributes.
04:55 You can always turn them into properties if needed. Use properties when you need to attach behavior to attributes and keep using them as regular attributes in your code. And avoid side effects in properties because no one would expect operations such as assignments to cause any side effects. Python’s properties are useful, but because of that, people tend to overuse them. In general, you should only use properties when you need to add extra processing on top of a specific attribute.
05:25 Turning all of your attributes into properties will be a waste of your time. It may also create performance and maintainability issues. In the next section of the course, you’ll take a look at more advanced tools you can use to replace getter and setter methods in Python.
Become a Member to join the conversation.