看廖下廖大的裝飾器的文件
寫了練習**【設計乙個decorator,它可作用於任何函式上,並列印該函式的執行時間】
# -*- coding: utf-8 -*-
import time, functools
def metric(fn):
time1=time.time()
ret = fn(*args, **kw)
time2=time.time()
print('%s executed in %.3f s' % (fn.__name__, time2 - time1))
return ret
# 測試
@metric
def fast(x, y):
time.sleep(1.23)
return x + y;
@metric
def slow(x, y, z):
time.sleep(2.1234)
return x * y * z;
f = fast(11, 22)
s = slow(11, 22, 33)
if f != 33:
print('測試失敗!')
elif s != 7986:
print('測試失敗!' )
python 裝飾器練習
import time import functools def add log fun functools.wraps fun start time time.time res fun args,kwargs end time time.time fun.name end time start t...
python裝飾器練習
判斷是否是root使用者 def hahaha fc def woshinima args if args 0 root args是列表,儘管只有乙個元素,但是必須是 0 return fc args print error return woshinima hahaha def add stude...
python裝飾器練習
裝飾器的初步使用 需求 1.登入京東 2.有3個頁面home finance book 3.檢測有沒有登入,沒登入呼叫登入介面 4.home使用京東自己的登入 auth type jingdong def login auth type,flag false deflogged f def hadl...