装饰器模型

1.必须使用装饰器,以保证上线的函数不产生不可预测的连锁反应

2.游戏规则:不能修改源代码、不能修改函数名

3.装饰器基本模型:

import  time

def b(f):
    def c(*g,**h):
        x=time.time()
        d=f(*g,**h)
        y=time.time()
        print(y-x)
        return d
    return c

@b
def a(g,h):
    time.sleep(3)
    print('%s%s'%(g,h))
    return 'ok'

@b
def a1(g,h,i):
    time.sleep(3)
    print('%s%s%s'%(g,h,i))
    return 'ok'

e=a(1,2)
f=a1(1,2,3)
print(e,f)
原文地址:https://www.cnblogs.com/shengbei/p/9022683.html