Welcome to a tutorial on Python Decorators. Here you will learn about decorators in python as special functions which add additional functionality to an existing function or code.
Suppose, you had a white car with a basic wheel setup and a mechanic changes the color of your car to red, fits alloy wheels to it, and the mechanic decorated your car. Also, python is sued to decorate (or add functionality or feature) to an already existing program.
Let’s learn about the concept of decorators before proceeding with the tutorial.
Know that in Python everything is regarded as an object and can be referenced using a variable name, including functions with attributes.
Also, multiple variables can reference the same function object (or definition). Check out the example below.
def one(msg):
print(msg)
# calling the function
one("Hello!")
# having a new variable reference it
two = one
# calling the new variable
two("Hello!")
Output:
Hello!
Hello!
In addition, we can as well pass a function as an argument. Check out the example below.
# some function
def first(msg):
print(msg)
# second function
def second(func, msg):
func(msg)
# calling the second function with first as argument
second(first, "Hello!")
Output:
Hello!
From the example above, the second function took the function first as an argument and used it, as a function can also return a function.
In cases where there are nested functions that is, function inside another function, and the outer function returns the inner function, we have what is known as a Closure in Python, which has been learned previously. Thus, in Python, Decorators can be regarded as an extension of the concept of closures.
As discussed above, decorators give a function new behavior without changing the function itself. It is used to add functionality or a class, as they wrap another function and extends the behavior of the wrapped function without necessarily modifying it permanently.
Check out the example below:
# a decorator function
def myDecor(func):
# inner function like in closures
def wrapper():
print("Modified function")
func()
return wrapper
def myfunc():
print('Hello!!')
# Calling myfunc()
myfunc()
# decorating the myfunc function
decorated_myfunc = myDecor(myfunc)
# calling the decorated version
decorated_myfunc()
Output:
Modified function
Hello!!
In the above example, we assumed the closure approach, however, rather than a variable, we passed a function as an argument, thus executing the function with some more code statements. That is, we passed the myfunc function as an argument to the function myDecor to get the decorated version of the function myfunc.
So, instead of passing the function as an argument to the decorator function, the python program will provide us with an easy way of doing that, by making use of the @ symbol. Check out the example below.
# using the decorator function
@myDecor
def myfunc():
print('Hello!!')
# Calling myfunc()
myfunc()
Output:
Modified function
Hello!!
From the above example, @myDecor was used to attach the myDecor() decorator to the required function. As such, if we call the myfunc() function, rather than it executing the actual body of the myfunc() function, it will be passed as an argument to myDecor() and the modified version of myfunc() returned will be executed.
In summary, the @<Decorator_name> is used to attach any decorator with the name Decorator_name to any function in python.
In the above sessions, we learned the use of decorators to modify the function that hasn't used any argument. Here will learn how to use the argument with a function that is to be decorated.
For this purpose, the *args and **kwargs will be used as the arguments in the inner function of the decorator.
The *args in the function definition is simply used to pass a variable number of arguments to any function, and also used to pass a non-keyworded, variable-length argument list.
The **kwargs in function definitions are typically employed to pass a keyworded, variable-length argument list. also, it has double stars (i.e. **kwargs), because the double stars allow one to pass keyword arguments (of any amount/number) through.
Check out the example below:
def myDecor(func):
def wrapper(*args, **kwargs):
print('Modified function')
func(*args, **kwargs)
return wrapper
@myDecor
def myfunc(msg):
print(msg)
# calling myfunc()
myfunc('Hey')
Output:
Modified function
Hey
From the above example, the function myfunc() takes an argument msg, which will be printed as the message. As such the call will result in the decorating of the function by the myDecor decorator and the argument passed to it will be as a result passed to the args of the wrapper() function, which will pass those arguments again while calling myfunc() function. Then, the message passed will be printed finally after the statement's modified function.
With chaining, more than one decorator can be used to decorate a function. Check out the example below.
def star(f):
def wrapped():
return '**' + f() + '**'
return wrapped
# second decorator
def plus(f):
def wrapped():
return '++' + f() + '++'
return wrapped
@star
@plus
def hello():
return 'hello'
print(hello())
Output:
**++hello++**
From the example above, the star and the Plus decorators were defined that can add the ** and ++ to our message. They are attached to the function hello(), thus they simultaneously modified the function decorating the output message.
The Decorators are commonly used for adding the timing and logging functionalities to the normal functions in a python program. Check out the example below:
import time
def timing(f):
def wrapper(*args, **kwargs):
start = time.time()
result = f(*args,**kwargs)
end = time.time()
print(f.__name__ +" took " + str((end-start)*1000) + " mil sec")
return result
return wrapper
@timing
def calcSquare(numbers):
result = []
for number in numbers:
result.append(number*number)
return result
@timing
def calcCube(numbers):
result = []
for number in numbers:
result.append(number*number*number)
return result
# main method
if __name__ == '__main__':
array = range(1,100000)
sq = calcSquare(array)
cube = calcCube(array)
Output:
calcSquare took 60.42599678039551 mil sec
calcCube took 52.678823471069336 mil sec
From our example above, two functions: calcCube and calcSquare were created and used to calculate the cube and square of a list of numbers respectively. So, if want to calculate the time taken to execute both functions, we did define a decorator timing to do that.
Therefore, we used the time module and the time before starting a function to start the variable and the time after a function ends to end the variable. The f.__name__ provides the name of the current function that is being decorated. As the code range(1,100000) returned a list of numbers from 1 to 100000.