Python @property decorator

braindead
Dev Genius
Published in
3 min readOct 21, 2022

--

In Python, a decorator feature wraps a function, appends several functionalities to existing code, and then returns it. Methods and functions are callable because they can be called. As a result, a decorator is a callable that returns a callable. This is also known as metaprogramming because a section of the programme changes another section of the programme at compile time. Note: For more information, see Decorators in Python.

Python @property decorator

@property decorator is a Python built-in decorator that allows you to define properties without having to call the inbuilt function property (). Which is used to return the property attributes of a class as parameters from the specified getter, setter, and deleter. Let’s look at some examples of how to use the @property decorator in Python: Exemplification 1:

# Python program to illustrate the use of# @property decorator# Defining classclass Portal:# Defining __init__ methoddef __init__(self):self.__name =''# Using @property decorator@property# Getter methoddef name(self):return self.__name# Setter method@name.setterdef name(self, val):self.__name = val# Deleter method@name.deleterdef name(self):del self.__name# Creating objectp = Portal();# Setting namep.name = 'CarToon'# Prints nameprint (p.name)# Deletes namedel p.name# As name is deleted above this# will throw an errorprint (p.name)

Output:

CarToon## An error is thrownTraceback (most recent call last):File "main.py", line 42, inprint (p.name)File "main.py", line 16, in namereturn self.__nameAttributeError: 'Portal' object has no attribute '_Portal__name'

The @property decorator is used here to define the property name in the class Portal, which has three methods (getter, setter, and deleter) with similar names, namely name(), but with different number of parameters. Whereas name(self) is a getter method and is labelled with @property, name(self, val) is a setter method because it is used to set the value of the attribute __name and is labelled with @name.setter. Finally, the method denoted by @name. deleter is a deleter method that can remove the value assigned by the setter method. However, deleter is called using the keyword del. Exemplification 2:

Python program to illustrate the use of# @property decorator# Creating classclass Celsius:# Defining init method with its parameterdef __init__(self, temp = 0):self._temperature = temp# @property decorator@property# Getter methoddef temp(self):# Prints the assigned temperature valueprint("The value of the temperature is: ")return self._temperature# Setter method@temp.setterdef temp(self, val):# If temperature is less than -273 than a value# error is thrownif val < -273:raise ValueError("It is a value error.")# Prints this if the value of the temperature is setprint("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 = -300print(cel.temp)

Output:

The value of the temperature is set.The value of the temperature is:-270# An error is thrownTraceback (most recent call last):File "main.py", line 47, incel.temp = -300File "main.py", line 28, in tempraise ValueError("It is a value error.")ValueError: It is a value error.

A value error is thrown here because the temperature assigned must be greater than -273. But it’s -300 degrees here. As a result, a value error is thrown.

--

--

A passionate programmer, I am eager to challenge myself to do things I’ve never accomplished before and I strive to learn and improve on my skills every day