Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Decorators with Arguments | Advanced Decorator Techniques
Python Decorators Explained

bookDecorators with Arguments

When you want your decorator to be more flexible or configurable, you may need it to accept its own arguments. This is common when you want to customize the behavior of your decorator for different situations. For example, you might want to log messages at different levels, restrict access to certain users, or set timeouts. To achieve this, you use a pattern called a decorator factory: a function that returns a decorator, allowing you to pass arguments to the decorator itself.

123456789101112131415
def repeat(times): def decorator(func): def wrapper(*args, **kwargs): result = None for _ in range(times): result = func(*args, **kwargs) return result return wrapper return decorator @repeat(3) def greet(name): print(f"Hello, {name}!") greet("Alice")
copy

Understanding how decorators with arguments work requires following the flow of function calls. When you use a decorator with arguments, such as @repeat(3), Python first calls the outermost function (repeat) with the provided argument (3). This function returns the actual decorator (decorator), which is then applied to the target function (greet). Inside the decorator, a wrapper function (wrapper) is defined to modify the behavior of the original function. The wrapper can access both the decorator argument (times) and the arguments passed to the decorated function (name). This layered structure allows you to create highly customizable decorators that adapt their behavior based on the arguments you provide.

question mark

Which of the following correctly describes the structure of a decorator that accepts arguments?

Select the correct answer

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 2. Capitolo 1

Chieda ad AI

expand

Chieda ad AI

ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

bookDecorators with Arguments

Scorri per mostrare il menu

When you want your decorator to be more flexible or configurable, you may need it to accept its own arguments. This is common when you want to customize the behavior of your decorator for different situations. For example, you might want to log messages at different levels, restrict access to certain users, or set timeouts. To achieve this, you use a pattern called a decorator factory: a function that returns a decorator, allowing you to pass arguments to the decorator itself.

123456789101112131415
def repeat(times): def decorator(func): def wrapper(*args, **kwargs): result = None for _ in range(times): result = func(*args, **kwargs) return result return wrapper return decorator @repeat(3) def greet(name): print(f"Hello, {name}!") greet("Alice")
copy

Understanding how decorators with arguments work requires following the flow of function calls. When you use a decorator with arguments, such as @repeat(3), Python first calls the outermost function (repeat) with the provided argument (3). This function returns the actual decorator (decorator), which is then applied to the target function (greet). Inside the decorator, a wrapper function (wrapper) is defined to modify the behavior of the original function. The wrapper can access both the decorator argument (times) and the arguments passed to the decorated function (name). This layered structure allows you to create highly customizable decorators that adapt their behavior based on the arguments you provide.

question mark

Which of the following correctly describes the structure of a decorator that accepts arguments?

Select the correct answer

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 2. Capitolo 1
some-alt