Introduction to Python @property decorator
The function which can be passed as a parameter to another function is called first order object in python and decorator is one such function which takes another function as an argument and in order to be able to easily define the properties without the need to manually call the property() function, we make use of @property decorator property in python and this decorator extends the behavior of the function which is passed as an argument to the function without causing any changes to the function.
Syntax:
def decoratorfunctionname(somefunctionname):
def wrapperfunctionname():
somefunctionname()
return wrapperfunctionname
Where some function name is the name of the function that is passed as an argument to the decorator function, the Wrapper function name is the name of the function that wraps some function name function which is passed as an argument and extends its behavior, Somefunctionname is the name of the function which is necessary to wrap the some function name function and Finally, the wrapper function is returned.
Working of @property decorator in Python
Working of @property decorator in Python is as follows:
- Whenever there is a need to be able to easily define the properties without the need to manually call the property() function, we make use of @property decorator property in python.
- Decorator is a function which takes another function as an argument.
- The behavior of the function passed as an argument to the decorator function is extended without causing any changes to the function passed as the argument by using decorator function.
- The decorator function wraps the function passed as an argument to it using wrapper function, to extend its behavior and makes use of other necessary functions to extend the behavior of the function passed as an argument and then returns the wrapper function.
Examples of Python @property decorator
Following are the examples are given below:
Example #1
Python program to demonstrate @python decorator to be able to easily define the properties without the need to manually:
- Step 2: The execution of the decorator function begins from here with the function name passed as an argument
def decorator(functionname):
- Step 3: This inside function gets executed first within the decorator function
def insidefunction():
print("Inside function is executed first")
- Step 4: The function passed as an argument gets executed next as part of the decorator function.
functionname()
return insidefunction
@decorator
def functionpassedasanargument():
print("The function passed as an argument is executed next")
- #Step 1: The execution of the program begins here by calling this function which is passed as an argument to the decorator function
functionpassedasanargument()
The output of the above program is as shown in the snapshot below:
- Step 1: The execution of the program begins by calling the function functionpassedasanargument() which is passed as an argument to the decorator function decorator(functionname).
- Step 2: The execution of the decorator function decorator(functionname) begins as the second step in the program.
- Step 3: This inside function insidefunction() gets executed first within the decorator function decorator(functionname).
- Step 4: The function passed as an argument functionpassedasanargument() gets executed next as part of the decorator function decorator(functionname).
Example #2
Python program to demonstrate @python decorator to be able to easily define the properties without the need to manually:
import datetime
- Step 2: The execution of the decorator function begins from here with the function name passed as an argument
def decorator(functionname):
- Step 3: This inside function gets executed first within the decorator function
def yearfinder():
b = datetime.datetime.now()
print("The current year is")
print(b.year)
- Step 4: The function passed as an argument gets executed next as part of the decorator function
functionname()
return yearfinder
@decorator
def dayfinder():
a = datetime.datetime.now()
print("The current day is:")
print(a.strftime("%A"))
- Step 1: The execution of the program begins here by calling this function which is passed as an argument to the decorator function
dayfinder()
The output of the above program is as shown in the snapshot below:
- Step 1: The execution of the program begins by calling the function dayfinder() which is passed as an argument to the decorator function decorator(functionname).
- Step 2: The execution of the decorator function decorator(functionname) begins as the second step in the program.
- Step 3: This inside function yearfinder() gets executed first within the decorator function decorator(functionname).
- Step 4: The function passed as an argument dayfinder() gets executed next as part of the decorator function decorator(functionname).
Example #3
Python program to demonstrate @python decorator to be able to easily define the properties without the need to manually:
- Step 2: The execution of the decorator function begins from here with the function name passed as an argument
def decorator(functionname):
- Step 3: This inside function gets executed first within the decorator function
def maxfinder():
b = max(100,200,300)
print("The maximum value is")
print(b)
- Step 4: The function passed as an argument gets executed next as part of the decorator function
functionname()
return maxfinder
@decorator
def minfinder():
a = min(100,200,300)
print("The minimum value is:")
print(a)
- Step 1: The execution of the program begins here by calling this function which is passed as an argument to the decorator function
minfinder()
The output of the above program is as shown in the snapshot below:
- Step 1: The execution of the program begins by calling the function minfinder() which is passed as an argument to the decorator function decorator(functionname).
- Step 2: The execution of the decorator function decorator(functionname) begins as the second step in the program.
- Step 3: This inside function maxfinder() gets executed first within the decorator function decorator(functionname).
- Step 4: The function passed as an argument minfinder() gets executed next as part of the decorator function decorator(functionname).
Recommended Articles
This is a guide to Python @property decorator. Here we also discuss the introduction and working of @property decorator in python along with different examples and its code implementation. You may also have a look at the following articles to learn more –