Decorators
Background
Following are important facts about functions in Python that are useful to understand decorator
functions.
We know Decorators are a 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 similarly to
objects.
• In Python, we can define a function inside another function.
• In Python, a function can be passed as parameter to another function (a function can
also return another function).
• 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.
Example 1
# A Python program to demonstrate that a function
# can be defined inside another function and a
# function can be passed as parameter.
# Adds a welcome message to the string
def messageWithWelcome(str):
# Nested function
def addWelcome():
return "Welcome to "
# Return concatenation of addWelcome()
# and str.
return addWelcome() + str
# To get site name to which welcome is added
def site(site_name):
return site_name
print messageWithWelcome(site("Developer"))
Function Decorator
A decorator is a function that takes a function as its only parameter and returns a function. This is
helpful to “wrap” functionality with the same code over and over again. For example, above code
can be re-written as following. We use @func_name to specify a decorator to be applied on another
function.
Example 2
# Adds a welcome message to the string
# returned by fun(). Takes fun() as
# parameter and returns welcome().
def decorate_message(fun):
# Nested function
def addWelcome(site_name):
return "Welcome to " + fun(site_name)
# Decorator returns a function
return addWelcome
@decorate_message
def site(site_name):
return site_name;
# Driver code
# This call is equivalent to call to
# decorate_message() with function
# site("Developer") as parameter
print site("Developer")
Decorators can also be useful to attach data (or add attribute) to functions.
Example 3
# A Python example to demonstrate that
# decorators can be useful attach data
# A decorator function to attach
# data to func
def attach_data(func):
func.data = 3
return func
@attach_data
def add (x, y):
return x + y
# Driver code
# This call is equivalent to attach_data()
# with add() as parameter
print(add(2, 3))
print(add.data)
Using add function:
Approach:
In the code above, log_decorator is the decorator function that takes the add function as an
argument. It returns a new function called wrapper, which adds logging before and after calling add.
The @log_decorator syntax is a shorthand way of applying the log_decorator function to the add
function. When we call add, the logging statements will be printed to the console.
Ex1
def log_decorator(func):
def wrapper(*args, **kwargs):
print(f"Calling {func.__name__} with args={args} kwargs={kwargs}")
result = func(*args, **kwargs)
print(f"{func.__name__} returned {result}")
return result
return wrapper
@log_decorator
def add(a, b):
return a + b
result = add(2, 3)
Using add function extended the add operation for tuple
Ex2
def decorator_list(fnc):
def inner(list_of_tuples):
return [fnc(val[0], val[1]) for val in list_of_tuples]
return inner
@decorator_list
def add_together(a, b):
return a + b
print(add_together([(1, 3), (3, 17), (5, 5), (6, 7)]))
We can call decorator in two diferent way
# 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
============================================
# 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)()
Another example with passing parameter to decorator function
# 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 decorators
decorator_func(12, 15)(my_fun)('Developer', 'for', 'Developer')
This example also tells us that Outer function parameters can be accessed by the enclosed inner
function. Scope of variable is availble inside the inner function.
Example
# Python code to illustrate
# Decorators with parameters in Python (Multi-level Decorators)
def decodecorator(dataType, message1, message2):
def decorator(fun):
print(message1)
def wrapper(*args, **kwargs):
print(message2)
if all([type(arg) == dataType for arg in args]):
return fun(*args, **kwargs)
return "Invalid Input"
return wrapper
return decorator
@decodecorator(str, "Decorator for 'stringJoin'", "stringJoin started ...")
def stringJoin(*args):
st = ''
for i in args:
st += i
return st
@decodecorator(int, "Decorator for 'summation'\n", "summation started ...")
def summation(*args):
summ = 0
for arg in args:
summ += arg
return summ
print(stringJoin("I ", 'like ', "Geeks", 'for', "geeks"))
print()
print(summation(19, 2, 8, 533, 67, 981, 119))
Output:
Decorator for 'stringJoin'
Decorator for 'summation'
stringJoin started ...
I like Geeksforgeeks
summation started ...
1729