Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn 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

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 1

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

Can you explain how the decorator factory pattern works in this example?

What are some other use cases for decorators with arguments?

Can you show how to add logging to this decorator?

bookDecorators with Arguments

Swipe to show 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

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 1
some-alt