python-装饰器

python-装饰器_第1张图片


python-装饰器_第2张图片


import time
def djs():
    print('sooo...')
    time.sleep(2)
def show_time(f):
    def inner():
        start = time.time()
        f()
        end = time.time()
        print('spend %s'%(end - start))
    return inner
djs = show_time(djs)
djs()

python-装饰器_第3张图片


return inner()返回的是值,return inner返回的是函数,闭包,应该返回的是函数。

python-装饰器_第4张图片


python-装饰器_第5张图片

python-装饰器_第6张图片

python-装饰器_第7张图片

 *a传入的是无名参数(如1,2,3,ffe.....),**a传入的是有名参数(如a=1,name=abc.......)

python-装饰器_第8张图片

你可能感兴趣的:(python基础)