Python Property Decorator

In this tutorial, we will learn about the property decorator in Python. Previously, we have discussed decorators in Python; if you are familiar with decorators, you should read this tutorial before moving forward.

Let's have an introduction to property decorators.

Python @property Decorator

Let's understand it by an example - Suppose we have a class Employee who has three properties first_name, last_name, and department_name. And we have another function email() function that generates an email address for an employee using its first_name and last_name. Let's see the following code.

Example -

Now create the object of class and call the email() function.

Output:

First Name is: Bruce
Last Name is: Banner
Full Name is: Bruce Banner
[email protected]

In the above code, we have defined the three attributes first_name, last_name, and department_name and email() are the derived attributes.

The full_name is declared as the variable and email() is declared as a function. When we run the program, we get the first_name, last_name, and full_name. The full_name is derived from the first_name and the last_name so email is. Now, let's make some changes in the program.

Changing the first_name to Natasha and printing the full_name and email will give the following output.

Output:

Full Name is Bruce Banner
[email protected]

We can see that the first name is changed, and the email changes automatically. Still, the full_name doesn't change despite using the first_name attribute because email() is a function called when we want the email to be returned, while first_name is set at the time of initialization of the object. We can fix this problem by creating another function of the full_name we created for the email().

Example -

Output:

First Name is: Bruce
Last Name is: Banner
Bruce Banner
[email protected]
After changing the first name:
Natasha Banner
[email protected]

We get changed full_name and updated email, but it is not a Pythonic way of solving this problem. Here the @property decorator comes into play to resolve such a problem.

Using @property Decorator

The property decorator returns the property attributes of a class from the stated getter and setter and deletes them as a parameter. We will use the @property decorator to solve this problem. Now let's see the above example using the @property decorator.

Example -

Output:

First Name is: Bruce
Last Name is: Banner
Full Name is: Bruce Banner
Email is: [email protected]
After changing the first name:
Full Name is: Natasha Banner
Email is: [email protected]

Explanation -

In the above code, the @property decorator is used on the full_name function, and now this function changed into the attribute and can also work as a getter because of the @property decorator.

Use setter and deleter methods with @property Decorator

The function that applies the @property decorator is known as the getter. In the previous example, the full_name acts a getter. In this section, we will understand the getter and setter.

As the name suggests, the setter method set the value of the attributes and deleter method deleter method removes the attributes from memory. Let's implement a setter and getter method for full_name attributes.

Example -

Output:

21
Rishabh Pant
KL Rahul
Traceback (most recent call last):
  File "d:/Python Project/property.py", line 557, in 
    s1.name = ''
  File "d:/Python Project/bubble_sort.py", line 544, in name
    raise ValueError('Please Enter the Valid String')
ValueError: Please Enter the Valid String

Explanation -

In the above code, we have initialized a class Student where we passed name and age in the constructor. First, we created the age() method that applied the @property decorator; it will get the age and returns. Then we created the setter method for the age where we checked if the user entered the value as negative then raised an error. Then we created the deleter method for the age. The same thing we have done with the name attributes. In the setter method, we checked if the user enters the name as an empty string raises an error otherwise, set the given name.

Example - 2

Output:

Fullname is: Steve Rogers
Email address is:  [email protected]
Fullname of obj is:  Bruce Rogers
And email address: [email protected]
New Fullname of obj is: Peter Parker
Deleted the full_name

In the above code, we have created the getter, setter, and deleter using the property decorator.

The property() Function

We can use the property() function to create getters, setters, and deleter in-place of @property decorator. The syntax is given below.

Syntax -

Parameters -

  • fget() - It is used to get the attribute's value the same as the getters.
  • fset() - It is used to set the value of the attributes the same as the setters.
  • fdel() - It is used to delete the attribute value.
  • doc() - It represents the string that contains the documentation (docstring) for the attributes.

It returns a property attribute from the given getter, setter, and deleter.

Let's understand the following example.

Example -

Output:

Full Name is:  Nick Fury
Email Address:  [email protected]
Full Name is:   Nick Fury
Email Address:  [email protected]
New Full Name is:  Bruce Banner
Deleted the full_name.

We can also implement these methods using the single line of code.

full_name = property(full_name_getter, full_name_setter, full_name_deleter)

Important Tips

We don't need to create all three methods for every property in the code. We can define read-only properties by only including a getter method. We can also avoid the deleter method and use getter and setter. We can avoid the setter method if we want to set the attribute when the instance is created or if it should be modified within the class.

The users are free to choose any method depending on their working context.

Conclusion

This tutorial has covered property decorators and their examples in detail. The property() method helps create getter, setter, and deleter methods. We can define the properties with the @property decorator syntax, which is more compatible and straightforward. It is considered the 'pythonic' way to define getters, setters, and deleter.

Using the properties, we can modify the class's internal implementation without affecting it. The getter, setter, and deleter help avoid accessing or modifying the data directly.






Latest Courses