Declare @property in Python

Going back to the advanced Python lessons, today TipsMake.com will work with you to learn about @property (decorator) declarations.

Going back to the advanced Python lessons, today TipsMake.com will work with you to learn about @property (decorator) declarations. In the previous tutorial, we discussed Python Decorator, you should read it before going to learn about @property decorator .

@property decorator in Python

As mentioned, we can decorate a function by using decorator. There are some decorators built into Python, including @property. The main purpose of decorators is to change the methods and class properties so that there is no need to make any changes in the code.

Follow the following example:

 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) 

The output export program is as follows:

 Jaki 25 Jaki obtained 25 marks 

Now, what if we want to change the name attribute of the student class? Add the following 3 lines to the previous code:

 st.name = "Anusha" print(st.name) print(st.gotmarks) 

The program will return as follows:

 Jaki 25 Jaki obtained 25 marks Anusha Jaki obtained 25 marks 

Note that the name attribute has changed, but the sentence created by the gotmarks attribute is the same as it was when the student initialization process was placed .

Now we want gotmarks to change when the student name is updated. Please use @property decorator

We can solve this problem with the following code:

 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()) 

The output export program is as follows:

 Jaki 25 Jaki obtained 25 marks Anusha Anusha obtained 25 marks 

The request has been resolved. We removed the gotmarks attribute from the constructor and added a method called gotmarks() .

However, with this change, users who are using the class will have trouble because they need to replace all gotmarks attributes with the gotmarks() function. Assuming there are 1000 lines of code, the modification will be extremely complicated, lost and easily confused.

Solve the above problem by using @property decorator

We will solve this problem by using @property decorator python. Follow the following code:

 @property def gotmarks(self): return self.name + ' obtained ' + self.marks + ' marks' 

The program will provide the same output as before, remember to delete () behind the getmarks when printing the results. You just need to declare @property on the gotmarks() function so that it is available for future use.

With this decorator, you can continue to keep the old code content and extend the functionality for the function.

Set @property setter

In Python, use @property (decorators) to declare setter.

Now suppose we want to update the name and marks attribute when changing the value of the gotmarks, tracking the following code:

 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) 

To update the value of name and marks when changing the value of gotmarks, use the setter in @proprety decorator .

Note, writing @gotmarks.setter means that we are applying setter on gotmarks method , then we will separate the sentence and update the value of name and marks.

Using @proprety decorator with the above setter will produce the following output:

 Jaki 25 Jaki obtained 25 marks ################## Anusha Anusha obtained 25 marks ################## Golam obtained 36 marks Golam 36 

Previous lesson: Decorator in Python

Next lesson: Regular Expression (RegEx) in Python

Update 23 July 2019
Category

System

Mac OS X

Hardware

Game

Tech info

Technology

Science

Life

Application

Electric

Program

Mobile