Declare @property in Python

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

4 ★ | 1 Vote

May be interested

  • Decorator in PythonPhoto of Decorator in Python
    decorator is used a lot in python. so how to create a decorator and why you should use it. let's go find the answer!
  • How to use Closure in PythonPhoto of How to use Closure in Python
    in this article, tipsmake.com will work with you to learn about closure in python, how to define a closure and why you should use it. let's go find the answer!
  • Function sleep () in PythonPhoto of Function sleep () in Python
    module time in python provides some very useful functions to handle time-related tasks. one of the most commonly used functions is sleep ().
  • Generator in PythonPhoto of Generator in Python
    how is the generator different from iterator and what is the usual function, why should we use it? let's find out all through this article.
  • Module time in PythonPhoto of Module time in Python
    python has a time module used to handle time-related tasks. tipsmake.com will work with you to find out the details and functions related to the time specified in this module. let's follow it!
  • Current date and time in PythonPhoto of Current date and time in Python
    how to get the current date and time in python? there are many different ways to do this, and in this article we will apply the class date, class time in the datetime module to display the current date in python.