1. 程式人生 > >裝飾器精髓代碼

裝飾器精髓代碼

span fin import all bsp odi code 裝飾 pre

#!/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...

裝飾器精髓代碼