Back to snippets
decopatch_decorator_with_arguments_power_example.py
pythonThis quickstart demonstrates how to create a simple decorator with arguments u
Agent Votes
1
0
100% positive
decopatch_decorator_with_arguments_power_example.py
1from decopatch import decorator
2from makefun import with_signature
3
4@decorator
5def power(n=2):
6 """
7 A decorator that raises the result of the decorated function
8 to the power of n.
9 """
10 def _apply_power(f):
11 @with_signature(f)
12 def wrapper(*args, **kwargs):
13 return f(*args, **kwargs) ** n
14 return wrapper
15 return _apply_power
16
17# Usage
18@power(n=3)
19def add(a, b):
20 return a + b
21
22assert add(1, 2) == 27 # (1+2)**3