Python Property Decorator – @property

27 May, 2022
A decorator feature in Python wraps in a function, appends several functionalities to existing code and then returns it. Methods and functions are known to be callable as they can be called. Therefore, a decorator is also a callable that returns callable. This is also known as metaprogramming as at compile time a section of program alters another section of the program. Note: For more information, refer to Decorators in Python
@property decorator is a built-in decorator in Python which is helpful in defining the properties effortlessly without manually calling the inbuilt function property(). Which is used to return the property attributes of a class from the stated getter, setter and deleter as parameters. Now, lets see some examples to illustrate the use of @property decorator in Python: Example 1: 
Python
# Python program to illustrate the use of# @property decorator # Defining classclass Portal:     # Defining __init__ method    def __init__(self):        self.__name =''         # Using @property decorator    @property         # Getter method    def name(self):        return self.__name         # Setter method    @name.setter    def name(self, val):        self.__name = val     # Deleter method    @name.deleter    def name(self):       del self.__name # Creating objectp = Portal(); # Setting namep.name = 'GeeksforGeeks' # Prints nameprint (p.name) # Deletes namedel p.name # As name is deleted above this# will throw an errorprint (p.name)
GeeksforGeeks

## An error is thrown
Traceback (most recent call last):
  File "main.py", line 42, in 
    print (p.name)
  File "main.py", line 16, in name
    return self.__name
AttributeError: 'Portal' object has no attribute '_Portal__name'
Here, the @property decorator is used to define the property name in the class Portal, that has three methods(getter, setter, and deleter) with similar names i.e, name(), but they have different number of parameters. Where, the method name(self) labeled with @property is a getter method, name(self, val) is a setter method as it is used to set the value of the attribute __name and so its labeled with @name.setter. Lastly, the method labeled with @name.deleter is a deleter method which can delete the assigned value by the setter method. However, deleter is invoked with the help of a keyword del. Example 2: 
Python
# Python program to illustrate the use of# @property decorator # Creating classclass Celsius:         # Defining init method with its parameter    def __init__(self, temp = 0):        self._temperature = temp     # @property decorator    @property         # Getter method    def temp(self):                 # Prints the assigned temperature value        print("The value of the temperature is: ")        return self._temperature     # Setter method    @temp.setter    def temp(self, val):                 # If temperature is less than -273 than a value        # error is thrown        if val < -273:            raise ValueError("It is a value error.")                 # Prints this if the value of the temperature is set        print("The value of the temperature is set.")        self._temperature = val  # Creating object for the stated classcel = Celsius(); # Setting the temperature valuecel.temp = -270 # Prints the temperature that is setprint(cel.temp) # Setting the temperature value to -300# which is not possible so, an error is# throwncel.temp = -300
The value of the tempereture is set.
The value of the temperature is:
-270

# An error is thrown
Traceback (most recent call last):
  File "main.py", line 47, in 
    cel.temp = -300
  File "main.py", line 28, in temp
    raise ValueError("It is a value error.")
ValueError: It is a value error.
Here, a value error is thrown as the value of the temperature assigned must be above -273. But here it is -300. Hence, a value error is thrown.
vinayedula
Python Decorators
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Different ways to create Pandas Dataframe
Enumerate() in Python
Read a file line by line in Python
Python String | replace()
How to Install PIP on Windows ?
*args and **kwargs in Python
Python Classes and Objects
Python OOPs Concepts
Iterate over a list in Python
Introduction To PYTHON