Basics of Python Decorators

Learn the fundamental concepts of Python decorators with our beginner-friendly guide. Explore decorator syntax, usage, and practical examples to enhance your Python programming skills
E
Edtoks3:47 min read

Python decorators allow us to add extra functionality to the existing function. Syntax is shown below

@decorator
def test():
  # Add functionality here.
 

You can add a decorator by adding new functionality with @ operator and decorator name as shown above

Concept

The concept behind this is passing a function as an argument to another function. It is similar to function pointer in C and functors in C++

Function as an Argument 

In Python whenever you create a function, it stores in memory at some location. You can get memory location by just giving the function name. See the below code, without paratheses () , you will get memory location and call with paratheses. 

def test():
    print('This is test function')
test()
test
Output is
This is test function
<function __main__.test()>

 

Like assigning a value to a variable, a function can also be assigned to a variable, like function = test

Is Assigning Function Actual Copy or Hold Location?

To understand this, execute below and observe the outputs. 

def test():
    print('This is test function')

print('Calling test with test()')
test()

print('Printing test location with test')
test

print('Assigning test to a variable with function = test')
function = test

print('Calling function()')
function()

print('Now delete test with del test')
del test
print('Here calling test() failed')

print('Now call again with function()')
function()
print('It confirms that complete function definition is copied')

Output is 

Calling test with test()
This is test function
Printing test location with test
Assigning test to a variable with function = test
Calling function()
This is test function
Now delete test with del test
Here calling test() failed
Now call again with function()
This is test function
It confirms that complete function definition is copied

If it is difficult to understand with multiple prints, you test with individual lines and observe the output

def test():
    print('This is test function')

test()

test

function = test

function()

del test
# Here calling test() failed. Check by calling test()
test()

function()

You need to understand whether assigning is copying the function or not. so that you can test with the following code. Delete the test and call, then call again function

Similarly, you can pass a function as an argument to some other function.

def test():
    print('This is test function')
def test2(func):
    print('This is another function')
    func()
test2(test)
 

Output is

This is another function
This is test function

  Decorator

A decorator is an extra code to the existing function, meaning decorating the existing function. This can be achieved by passing a function as an argument. Below is the syntax

@decorator_function
def original_function():
  print('Original Function')

 

Here original_function is passed as an argument to the decorator_function. Now understand with an example

def decorator_function(orig_function):
    def decorator():
        print('Decorating orig_function, before calling')
        orig_function()
        print('Decorated orig_function, after calling')
    return decorator

@decorator_function
def original_function():
    print('This function needs decoration')

original_function()
 

Output is

Decorating orig_function, before calling
This function needs decoration
Decorated orig_function, after calling

 

Important Note: To use any function as a decorator it should return a function, as shown in the above example decorator_function is returning decorator function

You can turn the ON and OFF decorator by just comment the decorator as shown below.

#@decorator_function
def original_function():
    print('This function needs decoration')

original_function()

Output is

This function needs decoration

Let's keep in touch!

Subscribe to keep up with latest updates. We promise not to spam you.