Python property decorator - Python @property With Examples

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:

It will output as below:

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:

Then the output will be:

 Python property decorator.

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

It will output as follow:

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:

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:

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.

That’s all about python property decorator and python @property setter examples.

By admin

Leave a Reply