我想要一个特殊的装饰器,甚至内部更改python,以便可以实现以下功能:
@special_decorator
def fo():
pass
fo() # transform to " yield fo() "
答案 0 :(得分:0)
你可以使用这个装饰器:
def to_yield(f):
def inner():
yield f()
return inner
测试
@to_yield
def foo():
return 'called foo'
print(next(foo())
# prints "called foo"