I am trying to create a decorator from a chain of calls. It does not seem to be supported by syntax.
from functools import wraps
class Bar:
def wrapper(self):
def _outer(fun):
@wraps(fun)
def _f(*a, **kw):
print('I am in decorator')
return fun(*a, **kw)
return _f
return _outer
def foo():
return Bar()
# @foo().wrapper() # Invalid syntax
# def f():
# pass
# @(foo().wrapper()) # Invalid syntax
# def f():
# pass
def f():
pass
f = foo().wrapper()(f)
f()
Am I missing something? For some reasons, such a thing would be extremely useful in my project.
Thanks
You could do this:
wrapper = foo().wrapper()
@wrapper
def foo():
pass
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.