Decorators with parameters in Python
Prerequisite : Decorators in Python
Python functions are First Class citizens which means that functions can be treated similar to objects.
- Function can be assigned to a variable i.e they can be referenced.
- Function can be passed as an argument to another function.
- Function can be returned from a function.
Decorators with parameters is similar to normal decorators.
Syntax for decorators with parameters
@decorator(params)
def func_name():
''' Function implementation'''
The above code is equivalent to
def func_name():
''' Function implementation'''
func_name = (decorator(params))(func_name)
"""
As the execution starts from left to right decorator(params) is called which returns a function object fun_obj. Using the fun_obj the call fun_obj(fun_name) is made. Inside the inner function, required operations are performed and the actual function reference is returned which will be assigned to func_name. Now, func_name() can be used to call the function with decorator applied on it.
Above code can be visualized step by step here
How Decorator with parameters is implemented
def decorators(*args, **kwargs): def inner(func): ''' do operations with func ''' return func return inner #this is the fun_obj mentioned in the above content @decorators(params) def func(): """ function implementation """ |
Here params can also be empty.
Above code can be visualized step by step here
Example
# Python code to illustrate # Decorators with parameters in Python def decorator(*args, **kwargs): print("Inside decorator") def inner(func): print("Inside inner function") print("I like", kwargs['like']) return func return inner @decorator(like = "geeksforgeeks") def func(): print("Inside actual function") func() |
Output
Inside decorator Inside inner function I like geeksforgeeks Inside actual function
This example also tells us that Outer function parameters can be accessed by the enclosed inner function.
1. Inside the Decorator

2. Inside the function

The above example can be visualized step by step here.
Note : Image snapshots are taken using PythonTutor
Recommended Posts:
- Decorators in Python
- Memoization using decorators in Python
- Function Decorators in Python | Set 1 (Introduction)
- Python | Initialize tuples with parameters
- Data Classes in Python | Set 2 (Decorator Parameters)
- Python | Index of Non-Zero elements in Python list
- Python | Merge Python key values to list
- Reading Python File-Like Objects from C | Python
- Python | Convert list to Python array
- Important differences between Python 2.x and Python 3.x with examples
- Python | Sort Python Dictionaries by Key or Value
- Python | Add Logging to a Python Script
- Python | Set 4 (Dictionary, Keywords in Python)
- Python | Add Logging to Python Libraries
- Python | Visualizing O(n) using Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.
Improved By : KundaRamaKrishna



