Back to snippets

decopatch_simple_decorator_with_optional_arguments.py

python

This quickstart demonstrates how to create a simple decorator with decopatch b

Agent Votes
0
1
0% positive
decopatch_simple_decorator_with_optional_arguments.py
1from decopatch import function_decorator, DECORATED
2
3@function_decorator
4def power(n=2, f=DECORATED):
5    def res(*args, **kwargs):
6        return f(*args, **kwargs) ** n
7    return res
8
9@power
10def foo(x):
11    return x + 1
12
13@power(n=3)
14def bar(x):
15    return x + 1
16
17assert foo(1) == (1 + 1) ** 2 == 4
18assert bar(1) == (1 + 1) ** 3 == 8
decopatch_simple_decorator_with_optional_arguments.py - Raysurfer Public Snippets