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

  • 5 choose the best Python IDE for you5 choose the best Python IDE for you
    in order to learn well python, it is essential that you find yourself an appropriate ide to develop. quantrimang would like to introduce some of the best environments to help improve your productivity.
  • What is Python? Why choose Python?What is Python?  Why choose Python?
    python is a powerful, high-level, object-oriented programming language, created by guido van rossum. python is easy to learn and emerging as one of the best introductory programming languages ​​for people who are first exposed to programming languages.
  • Module time in PythonModule 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!
  • Python data type: string, number, list, tuple, set and dictionaryPython data type: string, number, list, tuple, set and dictionary
    in this section, you'll learn how to use python as a computer, grasp python's data types and take the first step towards python programming.
  • How to install Python on Windows, macOS, LinuxHow to install Python on Windows, macOS, Linux
    to get started with python, you first need to install python on the computer you are using, be it windows, macos or linux. below is a guide to installing python on your computer, specific to each operating system.
  • How to set up Python to program on WSLHow to set up Python to program on WSL
    get started with cross-platform python programming by setting up python on the windows subsystem for linux. here's how to set up python for wsl programming.
  • Multiple choice quiz about Python - Part 4Multiple choice quiz about Python - Part 4
    continue python's thematic quiz, part 4 goes back to the topic core data type - standard data types in python. let's try with quantrimang to try the 10 questions below.
  • How to use Closure in PythonHow 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!
  • Functions in PythonFunctions in Python
    what is python function? how is the syntax, components, and function types in python? how to create functions in python? these questions will be answered in the python lesson below.
  • How to use GPT-3 with PythonHow to use GPT-3 with Python
    feel free to use cool gpt-3 technology with your own python scripts using openai's handy api. here are details on how to use gpt-3 with python.