Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Introduction to Decorators | Mastering Python Decorators
Functional Programming Concepts in Python

Introduction to Decorators

Sveip for å vise menyen

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")

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")

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?

Velg det helt riktige svaret

question mark

How does a decorator modify a function's behavior?

Velg det helt riktige svaret

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 4. Kapittel 1

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

Seksjon 4. Kapittel 1
some-alt