Course Content

Setters & Property Decorators | Python Tutorials For Absolute Beginners In Hindi #69

In this course we are working on PyCharm IDE. There are also many other options available like Spyder, Idle, Wing, etc. but we will go with PyCharm for this series and you will see its benefits in the upcoming tutorials. If you haven't download it yet then download it by clicking on the Download PyCharm. This will take you to PyCharm official site. For installation guidelines, check Downloading Python and PyCharm Installation tutorial.

 

I will recommend you installing the community version as the other one will expire after a month trail and after that you have to pay to use its features.

Moving forward let's just open our PyCharm and create a new file.

As always I am again going to repeat: not to name our file similar to a module name. The reason for that, I have discussed in Tutorial #45. You can give it a read or watch the video for further clarification.

We are going to name our file python tuts 69.py here. 

In today’s tutorial, we are going over four of our main topics, i.e. Getter, Setter, Deleter and Property decorator. Let us start with property decorator. Before discussing property decorators, we must have an understanding of decorators themselves. Decorators are functions that take another function as an argument, and their purpose is to modify the other function without changing it.

Note: For more information, visit the Tutorial #51, that is solely based on decorators. 

A property decorator is a built-in function in Python. Property decorator is a pythonic way to use getters and setters in object-oriented programming which comes from the Python property class. Theoretically speaking, Python property decorator is composed of four things, i.e. getter, setter, deleter and Doc. The first three are methods, and the fourth one is a docstring or comment.

 @property
 #def getter method

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

Property decorator is used for setting the parameters. In Oop, the setter is a very important part of the program as we can change the values passed in parameters easily. Else without a setter, it is not possible 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 we can update values for an older one.

@function_name.setter
#def function

 @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:

  • The syntax of defining @property is very concise and readable.
  • We can access instance attributes while using the getters and setter to validate new values. This will avoid accessing or modifying the data directly.
  • 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.

Summary:

In this tutorial, we have learned about property decorator, setters, getters, and deleters. Property decorator(@property) is a pythonic way of defining getters, setters, and deleters. Properties defined with the @property syntax is more compact and readable. When we define the properties, we can change the internal implementation of the class without changing the program.

Code as described/written in the video


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)

Comments(3)

deva89 5 months, 4 weeks ago
can u pls explain why automatically this change is not visible in derived instance variables like fullname or email. Is this logic related to heap and stack memory etc.
gazal 1 month, 4 weeks ago
this playlist is amazing
gazal 1 month, 4 weeks ago
this playlist is amazing

Resources

No downloadable resources for this video. If you think you need anything, please post it in the QnA!

Course Announcements

Any Course related announcements will be posted here