Decorators with parameters in Python

Prerequisite : Decorators in Python, Function Decorators

We know Decorators are very powerful and useful tool in Python since it allows programmers to modify the behavior of function or class. In this article, we will learn about the Decorators with Parameters with help of multiple examples.

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.

How Decorator with parameters is implemented

filter_none

edit
close

play_arrow

link
brightness_4
code

   
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
    """

chevron_right


Here params can also be empty.

Observe these first –

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python code to illustrate 
# Decorators basic in Python 
  
def decorator_fun(func): 
  print("Inside decorator"
  
  def inner(*args, **kwargs): 
    print("Inside inner function"
    print("Decorated the function"
    # do operations with func
      
    func()
      
  return inner 
  
@decorator_fun
def func_to(): 
    print("Inside actual function"
  
func_to() 

chevron_right


Another Way –

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python code to illustrate 
# Decorators with parameters in Python 
  
def decorator_fun(func): 
  print("Inside decorator"
  
  def inner(*args, **kwargs): 
    print("Inside inner function"
    print("Decorated the function"
      
    func()
      
  return inner 
  
  
def func_to(): 
    print("Inside actual function"
  
# another way of using decorators
decorator_fun(func_to)() 

chevron_right


Output:

Inside decorator
Inside inner function
I like geeksforgeeks
Inside actual function

 
Let’s move to another examples –

Example #1:



filter_none

edit
close

play_arrow

link
brightness_4
code

# Python code to illustrate 
# Decorators with parameters in Python 
  
def decorator(*args, **kwargs):
    print("Inside decorator")
      
    def inner(func):
          
        # code functionality here
        print("Inside inner function")
        print("I like", kwargs['like']) 
          
        func()
          
    # reurning inner function    
    return inner
  
@decorator(like = "geeksforgeeks")
def my_func():
    print("Inside actual function")

chevron_right


Output:

Inside decorator
Inside inner function
I like geeksforgeeks
Inside actual function

Example #2:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python code to illustrate 
# Decorators with parameters in Python 
  
def decorator_func(x, y):
  
    def Inner(func):
  
        def wrapper(*args, **kwargs):
            print("I like Geeksforgeeks")
            print("Summation of values - {}".format(x+y) )
  
            func(*args, **kwargs)
              
        return wrapper
    return Inner
  
  
# Not using decorator 
def my_fun(*args):
    for ele in args:
        print(ele)
  
# another way of using dacorators
decorator_func(12, 15)(my_fun)('Geeks', 'for', 'Geeks')

chevron_right


Output:

I like Geeksforgeeks
Summation of values - 27
Geeks
for
Geeks

This example also tells us that Outer function parameters can be accessed by the enclosed inner function.

1. Inside the Decorator

inside-the-decorator-python

2. Inside the function

inside-function-decorator-pythonNote : Image snapshots are taken using PythonTutor.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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