Python Language Decorators


Decorator functions are software design patterns. They dynamically alter the functionality of a function, method, or class without having to directly use subclasses or change the source code of the decorated function. When used correctly, decorators can become powerful tools in the development process. This topic covers implementation and applications of decorator functions in Python.


  • def decorator_function(f): pass # defines a decorator named decorator_function

  • @decorator_function
    def decorated_function(): pass # the function is now wrapped (decorated by) decorator_function

  • decorated_function = decorator_function(decorated_function) # this is equivalent to using the syntactic sugar @decorator_function


fThe function to be decorated (wrapped)