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
Python @property decorator
@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:
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
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.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Class as decorator in python
- Data Classes in Python | Set 2 (Decorator Parameters)
- Decorator Method - Python Design Patterns
- call() decorator in Python
- Dispatch Decorator in Python
- Create an Exception Logging Decorator in Python
- Implementing LRU Cache Decorator in Python
- Decorator to print Function call details in Python
- Creating Decorator inside a class in Python
- Context Manager Using @contextmanager Decorator
- Python | property() function
- Wand matte_color property in Python
- Wand virtual_pixel property - Python
- Important differences between Python 2.x and Python 3.x with examples
- Python | Set 4 (Dictionary, Keywords in Python)
- Python | Sort Python Dictionaries by Key or Value
- Python | Merge Python key values to list
- Reading Python File-Like Objects from C | Python
- Python | Add Logging to a Python Script
- Python | Add Logging to Python Libraries
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.