主要結合程序認識理解Python中的裝飾器。練習Python代碼的編寫。
簡單代碼
#!/usr/bin/env python
#-*- coding: utf-8 -*-
"""
@author: 烽火
@license: Apache Licence
@file: decorate.py
@time: 7/5/17 3:52 PM
"""
import time
"""
定義簡單的裝飾器,用來輸出程序運行的所用時間
"""
def timer(func):
def decor(*args):
start_time = time.time();
func(*args);
end_time = time.time();
d_time = end_time - start_time
print("run the func use : ", d_time)
return decor;
@timer #printSth = timer(printSth) -> printSth = decor
def printSth(str, count):
for i in range(count):
print("%d hello,%s!"%(i,str))
printSth("world", 100)