Created
March 27, 2023 23:25
-
-
Save FuruNov/b1a168b8895faa4314ffc214b7ce453b to your computer and use it in GitHub Desktop.
複数の関数へ一括に複数のデコレータを適用する Python プログラム
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from functools import wraps | |
| def decorator1(func): | |
| @wraps(func) | |
| def wrapper(*args, **kwargs): | |
| print("Decorator 1") | |
| return func(*args, **kwargs) | |
| return wrapper | |
| def decorator2(func): | |
| @wraps(func) | |
| def wrapper(*args, **kwargs): | |
| print("Decorator 2") | |
| return func(*args, **kwargs) | |
| return wrapper | |
| def apply_decorators_to_functions(decorators, functions): | |
| for func in functions: | |
| for decorator in reversed(decorators): | |
| func = decorator(func) | |
| globals()[func.__name__] = func | |
| def foo(): | |
| print("Inside foo") | |
| def bar(): | |
| print("Inside bar") | |
| apply_decorators_to_functions([decorator1, decorator2], [foo, bar]) | |
| foo() | |
| bar() |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
グローバルスコープにデコレートした関数を定義するので望ましくない