From this tutorial, you will be learning about Python Decorator. You will see what decorators are, how to create and use them with the help of examples.
Note: The syntax used here is for Python 3. You may modify it to use with other versions of Python.
- 1 What is Python Decorator?
- 2 Decorator syntax
- 3 How does Decorator work in Python?
- 4 Chaining Decorators
- 5 Decorator Examples
To Learn Python from Scratch – Read Python Tutorial
What is Python Decorator?
Decorators are a callable entity in Python that allows us to make modifications to functions or classes. The decorator works in an abstract style to extend or completely replace the behavior of an object.
By understanding how to create decorators, you make your code extensible and more readable. Also, you get the functions tailored to your needs without even changing their definitions.
Follow the below style:
def decoratorFunc(args): # Code to you want to execute ... @decoratorFunc def existingFunction(args): # Code that you want to decorate ...
Alternatively, you can try this way:
existingFunction = decoratorFunc(existingFunction)
How does Decorator work in Python?
Mainly, decorators are a construct that wraps a function, gives it a new functionality without changing the original code.
They return the output which may differ from the original behavior of the function.
They are good to use when a function has to operate differently in different situations.
Create your decorator:
A decorator is a function that may or may not return a function object.
We place it on top of the function that we want to decorate and pre-fix it with a @ sign.
So, let’s first create the decorator function.
def decoratorFunc(fn): return 10
The above decorator function returns an integer instead of a function.
So when you apply it to a function, it would get overridden altogether.
Check below how to specify a decorator to a function.
@decoratorFunc def existingFunc(): print("Hello World!")
Let’s now bring all the pieces together.
def decoratorFunc(fn): return 10 @decoratorFunc def existingFunc(): print("Hello World!") existingFunc()
When you run the above code, the following error occurs.
line 8, in existingFunc() TypeError: 'int' object is not callable
It is because the decorator replaced the existing function and forcing it to return “10” which is an integer, not a callable object.
NOTE – the decorator’s return value replaces the function, i.e., existingFunc.
By the way, if you want to run the function that we decorated, then make the decorator return it. Check the below code.
def decoratorFunc(fn): def callExistingFunc(): print("%s was called." % fn) fn() return callExistingFunc @decoratorFunc def existingFunc(): print("Hello World!") existingFunc()
In the above example, our decorator function is returning a function which prints the name of the decorated function and executes it.
The result of execution is as follows:
<function existingFunc at 0x0000000000705158> was called. Hello World!
The following diagram attempts to simplify the decorator concept for you.
Must Read – Functions in Python
We can decorate a function as many times as desired. In such a case, the decorators create a chain effect.
Typically, the decorator at the top hands over the control to the next, and continues in this way.
For illustration, check out the following code:
def top(func): def wrapper(*args, **kwargs): print("1" * 1) func(*args, **kwargs) print("1" * 1) return wrapper def middle(func): def wrapper(*args, **kwargs): print("2" * 2) func(*args, **kwargs) print("2" * 2) return wrapper def bottom(func): def wrapper(*args, **kwargs): print("3" * 3) func(*args, **kwargs) print("3" * 3) return wrapper @top @middle @bottom def myTest(anyString): print(anyString) myTest("Hello World!")
You can check that this example is using three decorators on the myTest() function. Below is the result after execution:
1 22 333 Hello World! 333 22 1
Simple decorator program to demonstrate:
def decorate(func): def first(): func() print("This is the First Program on Decorators.") return first def hello_not_decorated(): print("Hello World!.\n") print("This is an original function that is not decorated : ") hello_not_decorated() print("This is a decorated function :") @decorate def hello(): print("Hello World!.") hello()
This is an original function that is not decorated : Hello World!. This is a decorated function : Hello World!. This is the First Program on Decorators.
Decorate arithmetic operations:
def arithmetic_operations(func): def operators(a, b): func(a, b) print("The product is :", a*b) print("The division is :", a/b) print("The remainder is :", a%b) return operators print("This is a decorated function.") @arithmetic_operations def add_and_subtract(a, b): print("The addition is :", a + b) print("The subtraction is :", a - b) add_and_subtract(8, 4)
This is a decorated function. The addition is : 12 The subtraction is : 4 The product is : 32 The division is : 2.0 The remainder is :
Display multiple lines using chaining:
def Chaining_of_decorators(func): print("This is an example of chaining of decorators implementation.") def Decorator_demo(func): print("This tutorial is about Decorators.") print("This is a decorated function.") @Chaining_of_decorators @Decorator_demo def hello(): print("Hello World!") hello
This is a decorated function. This tutorial is about Decorators. This is an example of chaining of decorators implementation.
Pass arguments to a decorator:
def argument_for_decorator(argument1, argument2): def decorator(function): def wrapper(*args): print("%s%s" % (argument1, argument2)) function(*args) print("Congratulations. You decorated a function.") return wrapper return decorator @argument_for_decorator("Hello ", "World!") def print_args(*args): print("The Fibonacci Sequence upto number 8.") for arg in args: print(arg) print_args(1, 1, 2, 3, 5, 8)
Hello World! The Fibonacci Sequence upto number 8. 1 1 2 3 5 8 Congratulations. You decorated a function.
We wish the above Python Decorator tutorial would have given you a fair idea of using them in real Python programs.