Property in Python

By | January 24, 2022
Property in Python

Property in Python is a built-in decorator. Decorators in Python are functions that take other functions as arguments, perform some actions, and then return arguments. In this Python tutorial, we will discuss @property. Also, we will explain the use of setter and deleter methods.

What is Property in Python?

@property is an inbuilt decorator in Python that is used with class methods, and it is mainly used to increase the degree of data encapsulation.

With the help of the @property decorator, we can access a class method as a class attribute.

Code Example:

class Student():

    def __init__(self,name,mom,dad):

        self.name = name
        self.mom_name =mom
        self.dad_name =dad

    def father_name(self):
        return self.dad_name 
 
    @property
    def mother_name(self):
        return self.mom_name     

sam = Student('Sam','Linda','Harry')         

print(sam.name)
print(sam.father_name())
print(sam.mother_name)          # here mother_name is used as an attribute, but it is a method

Output:

Sam
Harry
Linda

Behind the code:

In the above code, we have made an instance sam of the Student() class and passed three arguments. Inside the Student() class, we have defined two methods, namely father_name() and mother_name(). Here, the mother_name() method has @property as a decorator, and that’s why we can print mother_name as a class attribute.

Vamware

@Property setter and delete

In the above example, we print the mother_name method as an attribute. With the @property decorator, we can only print the method as an attribute, but if we use an assignment operator with the mother_name attribute, it won’t work. Rather, it throws an error.

However, with the help of property setter, we can use an assignment operator with that attribute. We can also use the property deleter to delete the value that we have assigned.

Vamware

Code Example:

class Student():
    def __init__(self,name,mom,dad):
        self.name = name
        self.mom_name =mom
        self.dad_name =dad
    def father_name(self):
        return self.dad_name
    def father_setter(self,dad):
        self.dad_name = dad   
    @property
    def mother_name(self):
        return self.mom_name
    @mother_name.setter
    def mother_name(self,mom):
        self.mom_name= mom
    @mother_name.deleter
    def mother_name(self):
        print("Deleting Mother name")
        self.mom_name = None

sam = Student('Sam','Linda','Harry')         
print(sam.name)
print(sam.father_name())
print(sam.mother_name)          # here mother_name is a used as an attribute but it is a method


print("-------------Using assignment operator to set the mother_name and simple function to change the father_name-------------------")
sam.father_setter("Harry Smith")
sam.mom_name = "Linda Smith"
print(sam.father_name())
print(sam.mother_name)

print("-----------------------property deleter for mother_name------------------------------------------")

del sam.mother_name
print(sam.mother_name)

Output:

Sam
Harry
Linda
-------------Using assignment operator to set the mother_name and simple function to change the father_name-------------------
Harry Smith
Linda Smith

-----------------------property deleter for mother_name------------------------------------------
Deleting Mother name
None

Behind the code:

In the above code, we have used the @property decorator and its setter and deleter methods on the mother_name() method. With the help of the @property decorator, we were only able to print the mother_name() method as a class attribute.

With the help of mother_name.setter, we were able to use the assignment operator (=) with the mother_name() method. Also, with the help of mother_name.deleter, we were able to perform the deletion operation on the mother_name() method.

Vamware

Note: We redefine the function when we use the @property setter and deleter methods on the class method. In the above example, we have used the father_setter() method to mimic the working of mother.setter. With the help of @property and its setter and delete methods, we can use the class method as a class attribute.

Conclusion

That sums up our discussion on property in Python. It is a useful Python decorator that allows us to access a class method as a class attribute. Moreover, @property has several methods like deleter and setter.

Author: Vinay Singh

I am a Full Stack Developer with a Bachelor's Degree in Computer Science, who also loves to write technical articles that can help fellow developers.

Leave a Reply

Your email address will not be published.