Skip to content

Instantly share code, notes, and snippets.

@FuruNov
Created March 27, 2023 23:25
Show Gist options
  • Select an option

  • Save FuruNov/b1a168b8895faa4314ffc214b7ce453b to your computer and use it in GitHub Desktop.

Select an option

Save FuruNov/b1a168b8895faa4314ffc214b7ce453b to your computer and use it in GitHub Desktop.
複数の関数へ一括に複数のデコレータを適用する Python プログラム
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()
@FuruNov
Copy link
Author

FuruNov commented Mar 29, 2023

グローバルスコープにデコレートした関数を定義するので望ましくない

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment