Decorators

Download python eBook

Introduction

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.

Syntax

  • 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

Parameters

ParameterDetails
fThe function to be decorated (wrapped)

Related Examples

Stats

2177 Contributors: 30
Friday, July 14, 2017
Licensed under: CC-BY-SA

Not affiliated with Stack Overflow
Rip Tutorial: info@zzzprojects.com

Download eBook