Reputation: 1218
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
Upvotes: 1
Views: 44
Reputation: 599580
You could do this:
wrapper = foo().wrapper()
@wrapper
def foo():
pass
Upvotes: 3