Menu Close

Python Property Decorator – @property

Python Property Decorator

In this tutorial, you will learn all about Python property decorator. Python property decorator mostly used in getter and setter.

Python provides us a built-in decorator @property that is mostly used with Python getter and setter in Python object-oriented programming.

I strongly recommend you, before going through this tutorial, please read our Python decorators tutorial, so that you don’t have any confusion regarding Python decorators.

Let’s see little bit about Python decorators.

What is Decorator in Python?

A decorator in Python is used to enhance the functionality of existing code without changing the code.
Decorator is callable that returns also a callable.

Without @property Decorator.

Until now, When we had to access the methods of the class using an object, Then we had access like objectName.methodName followed by the parenthesis.

let’s understand by an example.

Example

class Person:
    def __init__(self, first, last):
        self.first_name = first
        self.last_name = last
        
    # method to print email
    def printEmail(self):
        email = f"My email is {self.first_name.lower() + self.last_name.lower()}@gmail.com"
        return email
        
        
        
# create object
p1 = Person('Vishvajit', 'Rao')

# access class method
email = p1.printEmail()
print(email)

Output

My email is [email protected]

As you can see in the about example, we have access class method printEmail using object p1 followed by parenthesis.

Python Property Decorator

Property decorator in Python is a built-in decorator that is written as @property, it can be used with any method of the class to use the method as a property. Basically, using Python property decorator you can access the class method as property or without parenthesis.

Example

class Person:
    def __init__(self, first, last):
        self.first_name = first
        self.last_name = last
        
    # method to print email
    @property
    def printEmail(self):
        email = f"My email is {self.first_name.lower() + self.last_name.lower()}@gmail.com"
        return email
      
        
# create object
p1 = Person('Vishvajit', 'Rao')

# access class method
email = p1.printEmail
print(email)

output

My email is [email protected]

In the above example, the @property decorator applies on printEmail() method. The printEmail() method returns the email of the person. So we can not use the printEmail() method as property or without parenthesis to get the value of email.

as shown in below.


# create object
p1 = Person('Vishvajit', 'Rao')

# access class method
email = p1.printEmail
print(email)

Python Property Setter

As you can see in the above example, we defined the printEmail() method as the property. We can only access the value of the printEmail property, But sometimes we want to modify the value of the property, But we can’t modify it.to modify the value of the printEmail property we define the setter method for the printEmail property.

property setter is able to change the value of the property.

Example


class Person:
    def __init__(self, first, last):
        self.first_name = first
        self.last_name = last
        
    # method to print email
    @property
    def printEmail(self):
        email = f"My email is {self.first_name.lower() + self.last_name.lower()}@gmail.com"
        return email
        
        
    # property setter 
    @printEmail.setter
    def printEmail(self, email):
        first_and_last = email.split('@')[0]
        self.first_name = first_and_last.split('.')[0]
        self.last_name = first_and_last.split('.')[1]
    
    
    # method for print full name
    def printFullName(self):
        full_name = f"My name is {self.first_name.capitalize()} {self.last_name.capitalize()}."
        return full_name
        
           
        
# create object
p1 = Person('Vishvajit', 'Rao')

# access class method
email = p1.printEmail
print(email)


# print full name
full_name = p1.printFullName()
print(full_name)


# set the value of printEmail property
p1.printEmail = "[email protected]"

# access class method
email = p1.printEmail
print(email)


# print full name
full_name = p1.printFullName()
print(full_name)

Output


My email is [email protected]
My name is Vishvajit Rao.
My email is [email protected]
My name is John Doe.

In the above example, we have used two printEmail methods. One is for getter and another is for the setter. The Python property setter method should be having a value parameter to assign the value for the property.

After property setter, you are completely able to modify the value of the property.

Python Property Deleter

As we can see all about Python property getter and setter in about examples. Now it time to define Python property deleter, so that we can easily delete the value of the property as well.

To delete the value of the property, we have to define a property deleter method, That is able to delete the value of the property.

Example: Python property deleter example

class Person:
    def __init__(self, first, last):
        self.first_name = first
        self.last_name = last
        
    # method to print email
    @property
    def printEmail(self):
        email = f"My email is {self.first_name.lower() + self.last_name.lower()}@gmail.com"
        return email
        
        
    # property setter 
    @printEmail.setter
    def printEmail(self, email):
        first_and_last = email.split('@')[0]
        self.first_name = first_and_last.split('.')[0]
        self.last_name = first_and_last.split('.')[1]
    
    
    # method for print full name
    def printFullName(self):
        full_name = f"My name is {self.first_name} {self.last_name}."
        return full_name
        
    # property deleter
    @printEmail.deleter
    def printEmail(self):
        del self.first_name
        del self.last_name
        
       
# create object
p1 = Person('Vishvajit', 'Rao')

# access class method
email = p1.printEmail
print(email)


# print full name
full_name = p1.printFullName()
print(full_name)


# Change the value of printEmail property
p1.printEmail = "[email protected]"

# access class method
email = p1.printEmail
print(email)


# print full name
full_name = p1.printFullName()
print(full_name)


# delete the property
del p1.printEmail

# print full name
full_name = p1.printFullName()
print(full_name)

Output

 My email is [email protected]
 My name is Vishvajit Rao.
 My email is [email protected]
 My name is john doe.
 Traceback (most recent call last):
   File "C:\Users\Vishvajit\Desktop\PF Article\Python Basic part1\test.py", line 63, in 
     full_name = p1.printFullName()
   File "C:\Users\Vishvajit\Desktop\PF Article\Python Basic part1\test.py", line 23, in printFullName
     full_name = f"My name is {self.first_name} {self.last_name}."
 AttributeError: 'Person' object has no attribute 'first_name'

Conclusion

That’s it.In this article, we have seen all about Python property decorator as well as we have seen setter, getter and deleter with the help of the examples.

property decorator in Python is one of the most important concepts to use the method of the class as the property. If you don’t want to use the instance method as the method, you can use the Python property decorator.

I hope you like this Python property decorator article, please share and keep visiting for further Python tutorials.

Previous Article
Next Article

Related Posts