假设这个装饰器代码:
def makeitalic(fn):
def wrapped():
return "<i>" + fn() + "</i>"
return wrapped
@makeitalic
def hello():
return "hello world"
print (hello())
<i>hello world</i>
我希望通过以下代码使这个输出更方便:
def makeitalic(fn):
def wrapped():
return "<i>" + fn() + "</i>"
return wrapped
def hello():
return "hello world"
hello()
'hello world'
makeitalic(hello)
<function makeitalic.<locals>.wrapped at 0x02C25AE0>
makeitalic(hello())
<function makeitalic.<locals>.wrapped at 0x02E902B8>
print(makeitalic(hello))
<function makeitalic.<locals>.wrapped at 0x02C25AE0>
但它只是返回obj。 有没有办法通过这种方法达到第一个代码输出?
答案 0 :(得分:3)
是的,有:
makeitalic(hello)()