Clear, practical technology insights BSOD Code Lookup · Windows Error Code Lookup · Wi-Fi Troubleshooting · PC Troubleshooting Checklist

@Property Decorator: Declare @Property in Python

Explore @Property Decorator: Declare @Property in Python, including the main concepts, relevant details, and practical considerations.

Table of Contents

This updated guide examines @Property Decorator: Declare @Property in Python and organizes the essential facts, background, and practical takeaways in clear American English.

@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 thegotmarksattribute is the same as it was when the student initialization process was placed .

Now we wantgotmarksto 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 thegotmarksattribute from the constructor and added a method calledgotmarks().

However, with this change, users who are using the class will have trouble because they need to replace allgotmarksattributes with thegotmarks()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 thegotmarks()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.settermeans 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

FAQ

What is @Property Decorator: Declare @Property in Python about?

It provides a structured overview of @property decorator, explains the main context, and highlights practical takeaways for readers.

Why does this topic matter?

Understanding the main concepts helps readers evaluate the issue, avoid common mistakes, and make better-informed decisions.

How should readers use this information?

Use the guidance as a practical starting point, confirm details that may have changed, and follow current product, safety, or security recommendations.

Discussion

Reader Comments 0

Sign in with email or Google to join the discussion.