MOCKSTACKS
EN
Questions And Answers

More Tutorials









Python Setters & Property Decorators


Use @property along with the getter method to access the value of the attribute.

A property decorator is used for setting the parameters. In OOP, the setter is an important part of the program as we can easily get the values passed in parameters. Without a setter, it is impossible to update the values passed as parameters during object creation. Setters are usually used in Oop to set the value of private attributes in a class.

Setters are a great way of performing encapsulation that we discussed in Tutorial #59 of our Python Tutorials for beginners course. So by using setter, our interaction gets limited to the decorator, making the use of encapsulation concept, which is the basis of Oop. We can set new values for a newer object or update values for an older one.

 @property
 #def getter method

@function_name.setter is a setter method with which we can set the value of the attribute
Deleter is used to delete the values passed as a parameter before. We can use a setter if we want to update or change the value, but we can not use it to delete the value. This is where deleter comes in; it removes the previous value and sets the variable equal to none. As in OOP, we do not completely erase the existence of some variable but sets it equal to none.

 # Deleter method 
@function_name.deleter

@function_name.deleter is a deleter method which can delete the assigned value by the setter method

Advantages of @property in Python:


Following are some advantages of using @property in Python:

1. The syntax of defining @property is very concise and readable.
2. We can access instance attributes while using the getters and setter to validate new values. This will avoid accessing or modifying the data directly.
3. By using @property, we can reuse the name of a property. This will prevent us from creating new names for the getters, setters, and deleters.

Example



class Employee:
    def __init__(self, fname, lname):
        self.fname = fname
        self.lname = lname
        # self.email = f"{fname}.{lname}@codewithharry.com"

    def explain(self):
        return f"This employee is {self.fname} {self.lname}"

    @property
    def email(self):
        if self.fname==None or self.lname == None:
            return "Email is not set. Please set it using setter"
        return f"{self.fname}.{self.lname}@codewithharry.com"

    @email.setter
    def email(self, string):
        print("Setting now...")
        names = string.split("@")[0]
        self.fname = names.split(".")[0]
        self.lname = names.split(".")[1]

    @email.deleter
    def email(self):
        self.fname = None
        self.lname = None


hindustani_supporter = Employee("Hindustani", "Supporter")
# nikhil_raj_pandey = Employee("Nikhil", "Raj")

print(hindustani_supporter.email)

hindustani_supporter.fname = "US"

print(hindustani_supporter.email)
hindustani_supporter.email = "this.that@codewithharry.com"
print(hindustani_supporter.fname)

del hindustani_supporter.email
print(hindustani_supporter.email)
hindustani_supporter.email = "Harry.Perry@codewithharry.com"
print(hindustani_supporter.email)



Conclusion

In this page (written and validated by ) you learned about Python Setters & Property Decorators . What's Next? If you are interested in completing Python tutorial, your next topic will be learning about: Python Object Introspection.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.