Change language

Decorators with parameters in Python

Python functions are first class citizens, which means that functions can be treated as objects.

  • A function can be assigned to a variable, i.e. they can be referenced.
  • A function can be passed as an argument to another function.
  • A function can be returned from a function.

Decorators with parameters are similar to regular decorators.

Syntax for decorators with parameters

 @decorator (params) def func_name (): ’’ ’Function implementation’’, 

The above code is equivalent

 def func_name (): ’’ ’Function implementation’’, func_name = (decorator (params)) (func_name) "" "

Since execution starts from left to right, the decorator (params) is called which returns the function object fun_obj . Using fun_obj, fun_obj (fun_name) . Inside the inner function, the necessary operations are performed and the actual reference to the function is returned, which will be assigned to func_name . Now func_name () can be used to call the function with applied m to it with a decorator.

The above code can be rendered step by step here

How Decorator is implemented with parameters

  

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 parameters can also be empty. 
The above code can be rendered step by step here

example

# Python code for illustration
# 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 = "pythonengineering" )

def func ():

print ( "Inside actual function" )

 
func ()

Exit

 Inside decorator Inside inner function I like pythonengineering Inside actual function 

This example also tells us that the parameters of an outer function can be accessed using a nested inner function.

1. Inside the decorator

2. Inside the function

The above example can be rendered step by step here
Note. Snapshots of images are taken with PythonTutor.

Shop

Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best laptop for development

$499+
Gifts for programmers

Best laptop for Cricut Maker

$299+
Gifts for programmers

Best laptop for hacking

$890
Gifts for programmers

Best laptop for Machine Learning

$699+
Gifts for programmers

Raspberry Pi robot kit

$150

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically