In Python, property decorators are used to define getter, setter, and deleter methods for class properties. They allow for the encapsulation of data, by controlling access to the underlying data. Property decorators are applied to methods and define how a property value can be retrieved, set, or deleted.
This program defines a Python class "student" with the following attributes and methods:
The program then creates an object o of the "student" class and passes 450 as the argument for total. It then prints the total attribute and the result of the average method.
Finally, it sets the value of total to 550, which is greater than 500, hence it is considered as invalid and "Invalid Total and can't Change." is printed.
# Property Decorators Getter Setter class student: def __init__(self, total): self._total = total def average(self): return self._total / 5.0 @property def total(self): return self._total @total.setter def total(self, t): if t < 0 or t > 500: print("Invalid Total and can't Change") else: self._total = t o = student(450) print("Total : ", o.total) print("Average : ", o.average()) o.total = 550 print("Total : ", o.total) print("Average : ", o.average())To download raw file Click Here
Total : 450 Average : 90.0 Invalid Total and can't Change Total : 450 Average : 90.0
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions