[英]how make handy decorator in python?
假定此裝飾器代碼:
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。 有什么辦法可以通過這種方法到達第一個代碼輸出?
就在這里:
makeitalic(hello)()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.