Python Language Decorators

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

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)


Got any Python Language Question?