简体   繁体   中英

how make handy decorator in python?

assume this decorator code :

first code:

def makeitalic(fn):
    def wrapped():
        return "<i>" + fn() + "</i>"
    return wrapped

@makeitalic
def hello():
    return "hello world"

print (hello())


<i>hello world</i>

I want to make this output handy by this code:

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>

but it just return obj. is there any way to reach the first code output by this method ?

就在这里:

makeitalic(hello)()

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM