Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Decorators with Arguments | Advanced Decorator Techniques
Quizzes & Challenges
Quizzes
Challenges
/
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

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. 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

bookDecorators with Arguments

Sveip for å vise menyen

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

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 1
some-alt