Advanced Lesson 4
Python Decorators
Chapter 3: Using decorators
Property decorator
You will most likely also be using decorators provided by Python.
The main one that you might have encountered is the @property
decorator. We discussed this in the Advanced OOP lesson.
Here is our version of the code that uses the @property
and @property_name.setter
decorator to encapsulate VainPerson
‘s name. I have simplified the setter method in this version just to make it easier to read.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
When you use the @property
and @property_name.setter
decorators, Python automatically converts them to something like the following code. Here, we create a property object called name
(Line 11), and link the property to the getter method get_name()
and the setter method set_name()
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
The property
class has a constructor, which takes a function (the getter method) as the first parameter, and optionally a setter method as the second parameter. Python will automatically invoke the correct methods when a user accesses the .name
attribute of a VainPerson
instance.
If you omit the setter method (the second argument), then your property becomes a read-only property.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|