Hello friends, today we will learn about Python property decorator. In our previous tutorial, we have discussed Python Decorator, you should read that if you are not familiar with the decorator before learning about python property decorator.
Python property decorator
As we discussed that we can decorate a function using decorators. There are some built-in decorators.
Python @property is one of the built-in decorators. The main purpose of any decorator is to change your class methods or attributes in such a way so that the user of your class no need to make any change in their code.
Consider the following class segment:
1 2 3 4 5 6 7 8 9 10 11 |
class Student: def __init__(self, name, marks): self.name = name self.marks = marks self.gotmarks = self.name + ' obtained ' + self.marks + ' marks' st = Student("Jaki", "25") print(st.name) print(st.marks) print(st.gotmarks) |
It will output as below:
1 2 3 4 5 |
Jaki 25 Jaki obtained 25 marks |
Now say we want to change the name attribute of the student class then what will happen? Append the following 3 line after the previous code:
1 2 3 4 5 |
st.name = "Anusha" print(st.name) print(st.gotmarks) |
Then the output will be:
Notice that the name attribute got changed but the sentence that was created by the gotmarks
attribute remained same as it was set during the initialization of the student object.
But we want gotmarks also to be changed when student name is updated. Here comes the use of python property decorator.
We can solve this problem with the following code.
Using Python Function to solve above problem
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
class Student: def __init__(self, name, marks): self.name = name self.marks = marks # self.gotmarks = self.name + ' obtained ' + self.marks + ' marks' def gotmarks(self): return self.name + ' obtained ' + self.marks + ' marks' st = Student("Jaki", "25") print(st.name) print(st.marks) print(st.gotmarks()) st.name = "Anusha" print(st.name) print(st.gotmarks()) |
It will output as follow:
1 2 3 4 5 6 7 |
Jaki 25 Jaki obtained 25 marks Anusha Anusha obtained 25 marks |
Wow!!! Our requirement is solved. But have a close look at the code. We have removed the gotmarks attribute from the constructor and added a method named gotmarks.
So now in our class there is no attribute named gotmarks
and we have a method named gotmarks()
.
And for this change, any user who is using my class will be in trouble as they have to replace all the attribute gotmarks with a function call gotmarks(). Assume there are 1000 lines of code then how troublesome it will be for the coder.
Solving above problem using Python property decorator
So we will solve this problem in pythonic way using the python property decorator. Notice the following code:
1 2 3 4 5 |
@property def gotmarks(self): return self.name + ' obtained ' + self.marks + ' marks' |
It will provide the same output as before, and don’t forget to remove the ‘()’ after gotmarks when printing it. Just writing @property above the function gotmarks() make it available to be used as a property.
And the user of our class doesn’t even know that attribute gotmarks is removed and we have a function for that. This is how the property decorator helps in keeping our code loosely coupled with the client code.
Python property setter
Now let’s say we want to set the name and marks attribute when we change the value of gotmarks. Deeply observe the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
class Student: def __init__(self, name, marks): self.name = name self.marks = marks # self.gotmarks = self.name + ' obtained ' + self.marks + ' marks' @property def gotmarks(self): return self.name + ' obtained ' + self.marks + ' marks' @gotmarks.setter def gotmarks(self, sentence): name, rand, marks = sentence.split(' ') self.name = name self.marks = marks st = Student("Jaki", "25") print(st.name) print(st.marks) print(st.gotmarks) print("##################") st.name = "Anusha" print(st.name) print(st.gotmarks) print("##################") st.gotmarks="Golam obtained 36" print(st.gotmarks) print(st.name) print(st.marks) |
As we want to update the value of name and marks when we are setting the value of gotmarks. So using the setter
of @proprety decorator we can achieve this.
Notice that we have written @gotmarks.setter
that means we are applying the setter on the gotmarks method. And then we are splitting the sentence and updating the value of name and marks.
The above python property decorator with setter will produce below output.
1 2 3 4 5 6 7 8 9 10 11 12 |
Jaki 25 Jaki obtained 25 marks ################## Anusha Anusha obtained 25 marks ################## Golam obtained 36 marks Golam 36 |
That’s all about python property decorator and python @property setter examples.