3.1. About Decorators

3.1.1. What are decorators?

  • Decorator is an object, which takes another object as it's argument

  • Decorators can:

    • Do things before call

    • Do things after call

    • Modify arguments

    • Modify returned value

    • Avoid calling

    • Modify globals

    • Add or change metadata

3.1.2. Types of decorators

By type:

  • Function Decorators

  • Class Decorators

By decorated object:

  • Decorating function

  • Decorating class

  • Decorating methods

By number of arguments:

  • Without arguments

  • With arguments

3.1.3. How decorator works

3.1.3.1. Without arguments

Decorator:
@my_decorator
def my_function(*args, **kwargs):
    pass
Is equivalent to:
my_function = my_decorator(my_function)

3.1.3.2. With arguments

Decorator:
@my_decorator(a, b)
def my_function(*args, **kwargs):
    pass
Is equivalent to:
my_function = my_decorator(a, b)(my_function)