How to Use Decorators in Python, by example

Originally posted on towardsdatascience

Decorators in Python

1. add_together function

1.1. Extending the functionality of add_together with a decorator

add_together = decorator_list(add_together)
Syntactically, @decorator_list is the same as passing function add_together to decorator_list, and re-assigning back to add_together

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)]))

# add_together = decorator_list(add_together)

2. Decorators that can take arguments themselves

An additional layer of function is added when we want to create decorators that take arguments themselves.
# Part 2
def meta_decorator(power):
    def decorator_list(fnc):
        def inner(list_of_tuples):
            return [(fnc(val[0], val[1])) ** power for val in list_of_tuples]
        return inner
    return decorator_list


@meta_decorator(2)
def add_together(a, b):
    return a + b


print(add_together([(1, 3), (3, 17), (5, 5), (6, 7)]))

3. Default arguments in Decorators

def meta_decorator(arg):
    def decorator_list(fnc):
        def inner(list_of_tuples):
            return [(fnc(val[0], val[1])) ** power for val in list_of_tuples]
        return inner
    if callable(arg):
        power = 2
        return decorator_list(arg)
    else:
        power = arg
        return decorator_list


@meta_decorator
def add_together(a, b):
    return a + b

# @meta_decorator(3)
# def add_together(a, b):
#     return a + b

4. Summary

Source: towardsdatascience

Share :