
In this tutorial, we will discuss one of the pythons inbuilt-decorators @property and also learn the use of getter and setter.
What is @Property in Python?
@property is an inbuilt-python decorator which is used with class methods, and it is mainly used to increase the data encapsulation concept in python.
With the help of a @property decorator, we can access a class method as a class attribute.
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 a 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 class Student() and passed three arguments. Inside the class Student, we have defined two methods father_name() and mother_name(), here mother_name() method has @property as a decorator.
As the mother_name() method has @property as a decorator that’s why we can print the mother_name as a class attribute.
@Property setter and delete
In the above example, we have print the mother_name method as an attribute. With only @property decorator, we can only print method as an attribute, but if we use an assignment operator with mother_name attribute it won’t work rather than it throws an error.
But 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 which we have assigned.
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 method setter and deleter on mother_name() method.
With the help of @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 mother_name() method.
With the help of mother_name.deleter we were able to perform the del keyword on mother_name() method.
Note: We redefine the function when we use the property setter and deleter method 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 method setter and delete we can use the class method as a class attribute.