DEV Community

Cover image for My look into Python Decorators.
Jerim Kaura
Jerim Kaura

Posted on • Updated on

My look into Python Decorators.

The concept of Python decorators is unclear to many people, even though it's simple. For this reason, you can consider Python decorators as high-level functions in Python which that takes another function as its argument and returns another function as a return value.

Python decorators show how the Python programming language packs several features in your small and beneficial package. Decorators can be applied to classes and functions to make programming fascinating. They can speed up the performance, shorten and completely change the dynamics of what the code can do.

The explanation may look abstract, but if we take an example, it will be manageable. Let's demonstrate these by writing two different functions and using them to explore the concept of Python decorators.

For instance, if we take our function above, we can decorate it as shown in the snippets below.

def umbrella_function(function):

    name = 'Jerim Kaura'
    def inner_function():
        print(name)
        function()
    return inner_function

def another_function():
    print('I am a computer science student')

x = umbrella_function(another_function)

x()
Enter fullscreen mode Exit fullscreen mode

Output:

Jerim Kaura
I am a computer science student
Enter fullscreen mode Exit fullscreen mode

In the example above umbrella_function()was a decorator but when we say x = umbrella_function(another_function) the function another_function() got decorated and the returned function was assigned to the variable x.

To decorate a function in Python, we use the @ symbol alongside the name of the decorator function and place it immediately above the function definition. For instance,

@umbrella_function
def another_function():
    print('I am a computer science student')
Enter fullscreen mode Exit fullscreen mode

The above is the same as

def another_function():
    print('I am a computer science student')

x = umbrella_function(another_function)
Enter fullscreen mode Exit fullscreen mode

Passing parameters to decorators

Sometimes we may need to define a decorator function that accepts parameters. We can achieve this by passing the parameters to the wrapper function, which we then pass to the function that is being decorated.

def umbrella_function(function):

    def inner_function(args1, args2):   
        print("Arguments passed are: {0}, {1}".format(args1,args2))     
        function(args1, args2)
    return inner_function

@umbrella_function
def another_function(name, age):
   print ('So, {name} is {age} years old and she is a {occupation}'
   .format(name = name, age = age, occupation = 'programmer'))

another_function('Jerry', 18)
Enter fullscreen mode Exit fullscreen mode

Output:

Arguments passed are: Jerry, 18
So, Jerry is 18 years old, and she is a programmer
Enter fullscreen mode Exit fullscreen mode

Further reading

To learn more about Python decorators, consider checking out how the concept is discussed in the Python's Decorator Library

Top comments (1)

Collapse
 
maen profile image
Ruheza, NS

Great stuff