Python - Decorators

April 13, 2018 Oclemy Python Function 0 minutes, 32 seconds

Decorators decorate functions. They themselves are functions that accept a function and return a function.

Decorators get applied using a special syntax.

def show_errors():

In the above the decorator is the @suppress_errors statement.

Decorators are important in that they allow a set of common tasks to be performed by many different functions, ususlaly before or after doing something more specific to the target function.

Common areas where decorators get applied include: access control, error handling, caching, logging, mapping routes to functions etc.

Without decorators you would need to copy code several times to perform that task before or after the target function is executed.