1. 程式人生 > >python裝飾器學習

python裝飾器學習

flag 局部變量 %d art lee ogg 參數 () time()

條件明白:變量-局部變量-全部變量
明白閉包:

#功能函數參數
def print_logger(flag = ‘‘):
    def count_time(fun):
        def inner(*x,**y):
            start_time = time.time()
            fun(*x,**y)
            end_time = time.time()
            print("時間:%d" % (end_time - start_time))
            if flag == ‘true‘:
                print("日誌記錄:%s,%s"%(time.time(),fun))
        return inner
    return count_time

@print_logger(‘‘)    #為了控制是否打印日誌
def addtwo(*a,**b):
    sum = 0
    for i in a:
        sum += i
    print(sum)
    time.sleep(1)

if __name__ == ‘__main__‘:
    addtwo(2,90,3,4,5,5)

python裝飾器學習