Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Leer Introduction to Decorators | Meesterschap in Python-Iterators en Decorateurs
Functionele Programmeerconcepten in Python

bookIntroduction to Decorators

Veeg om het menu te tonen

Note
Definition

A decorator in Python is a higher-order function that takes another function as its argument and returns a new function that extends or alters the behavior of the original function.

Decorators are a core feature in Python that allow you to modify or enhance the behavior of functions or methods without changing their code directly. At their heart, decorators are higher-order functions — they take another function as an argument, extend or alter its behavior, and return a new function. In Python, the syntax for applying a decorator is the @ symbol followed by the decorator name, placed directly above the function definition.

Common use cases for decorators include:

  • Logging;
  • Timing how long functions take to run;
  • Enforcing access control or permissions;
  • Memoization (caching results);
  • Input validation.

Decorators let you keep such logic separate from your core business logic, leading to cleaner and more maintainable code. By mastering decorators, you unlock a powerful tool for writing reusable and expressive Python code.

12345678910111213
def simple_decorator(func): def wrapper(*args, **kwargs): print("Before the function call") result = func(*args, **kwargs) print("After the function call") return result return wrapper @simple_decorator def greet(name): print(f"Hello, {name}!") greet("Alice")
copy

Understanding *args and **kwargs in Function Definitions

When writing decorators and wrappers, you often need to handle functions that take any number or type of arguments. Python provides *args and **kwargs to make this possible.

  • *args collects any extra positional arguments passed to a function into a tuple;
  • **kwargs collects any extra keyword arguments into a dictionary.

This flexibility is crucial for decorators. A decorator must be able to accept and forward all arguments to the original function, no matter how many or what kind they are. Without *args and **kwargs, your decorator would only work with functions that have a fixed signature, limiting reusability.

123456789101112
def my_decorator(func): def wrapper(*args, **kwargs): print("Arguments:", args) print("Keyword arguments:", kwargs) return func(*args, **kwargs) return wrapper @my_decorator def example(a, b, greeting="Hello"): print(f"{greeting}, {a} and {b}!") example(1, 2, greeting="Hi")
copy

This approach ensures your decorator can wrap any function, making your code both robust and reusable.

1. What is the purpose of the @ symbol in Python?

2. How does a decorator modify a function's behavior?

question mark

What is the purpose of the @ symbol in Python?

Selecteer het correcte antwoord

question mark

How does a decorator modify a function's behavior?

Selecteer het correcte antwoord

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 4. Hoofdstuk 1

Vraag AI

expand

Vraag AI

ChatGPT

Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.

Sectie 4. Hoofdstuk 1
some-alt