python装饰器精髓代码

#!/usr/bin/env python
#-*- coding:utf-8 -*-
import time
def foo(func):
    def inner():
        print('fs...')
        func()
        print('finally...')
    return inner

@foo   #f1 = foo(f1)==inner
def f1():
    time.sleep(1)
    print('f1....')

f1()

输出结果:

fs...
f1....
finally...

 

posted @ 2017-10-09 15:47  大漠之烟  阅读(177)  评论(0编辑  收藏  举报