三元运算 变量 = 条件返回True的结果 if 条件 else 条件返回False ,用于简单
import time
time.time() 1970年至今多少秒
time.sleep()暂时休眠
装饰器形成过程
def func():
time.sleep(0.01)
print('fds')
def timmer(f): #装饰器函数
def inner():
start = time.time()
f() #被装饰的函数
end = time.time()
print(end - start)
return inner
func = timmer(func)
func()
View Code
timer就是一个装饰器函数,只对一个函数,有一些装饰作用
装饰器作用
不想修改函数的调用方式,但还想在原来的函数前后添加功能
原则
开发封闭原则
开放:对扩展是开放的
封闭:对修改是封闭的
封版
语法糖
def timmer(f): #装饰器函数名
def inner():
start = time.time()
f() #被装饰的函数
end = time.time()
print(end - start)
return inner
@timmer #语法糖
def func():#被装饰的函数
time.sleep(0.01)
print('fds')
# func = timmer(func)
func()
View Code
def timmer(f): #装饰器函数名
def inner():
start = time.time()
ret = f() #被装饰的函数
end = time.time()
print(end - start)
return ret
return inner
@timmer #语法糖
def func():#被装饰的函数
time.sleep(0.01)
print('fds')
return 'hhh'
# func = timmer(func)
ret = func() #inner
print(ret)
#fds
# 0.01003718376159668
# hhh
View Code
装饰带参数函数的装饰器
def timmer(f): #装饰器函数名
def inner(a):
start = time.time()
ret = f(a) #被装饰的函数
end = time.time()
print(end - start)
return ret
return inner
@timmer #语法糖
def func(a):#被装饰的函数
time.sleep(0.01)
print('fds',a)
return 'hhh'
# func = timmer(func)
ret = func(1) #inner
print(ret)
# fds 1
# 0.010416269302368164
# hhh
View Code
def timmer(f): #装饰器函数名
def inner(*args,**kwargs):
start = time.time()
ret = f(*args,**kwargs) #被装饰的函数
end = time.time()
print(end - start)
return ret
return inner
@timmer #语法糖
def func(a,b):#被装饰的函数
time.sleep(0.01)
print('fds',a,b)
return '111'
@timmer #语法糖
def func1(a):#被装饰的函数
time.sleep(0.01)
print('fds',a)
return '222'
# func = timmer(func)
ret = func(1,2) #inner
ret1 = func1(1)
# print(ret,ret1)
# fds 1 2
# 0.010915040969848633
# fds 1
# 0.01041555404663086
# 111 222
View Code
装饰器的固定模式
def wrapper(func):
def inner(*args,**kwargs):
ret = func(*args,**kwargs) #被装饰函数
return ret
return inner()
@wrapper #qq = wrapper(qq)
def qq():
print(123)
ret = qq() #相当于在执行inner()
View Code
def wrapper(func):
def inner(*args,**kwargs):
print('在被装饰器之前做的事:')
ret = func(*args,**kwargs)
print('在被装饰的函数执行之后做的事:')
return ret
return inner
@wrapper
def h(day):
print("gg")
return 'tyw'
ret = h(3)
print(ret)
# 在被装饰器之前做的事:
# gg
# 在被装饰的函数执行之后做的事:
# tyw
View Code
def outer(*args,**kwargs):
print(args)
print(*args)
def inner(*args):
print('inner',args)
inner(*args)
outer(1,2,3) # ==outer(#[1,2,3,4])
# (1, 2, 3)
# 1 2 3
# inner (1, 2, 3)
View Code
def wa():
'''
一个哈哈哈的函数
:return:
'''
print('哈哈哈')
print(wa.__name__) #查看字符串的函数名
print(wa.__doc__) #查看函数注释
# wa
#
# 一个哈哈哈的函数
# :return
View Code
def wrapper(func): #func = h
def inner(*args,**kwargs):
print('在被装饰器之前做的事:')
ret = func(*args,**kwargs)
print('在被装饰的函数执行之后做的事:')
return ret
return inner
@wrapper
def h(day):
print("gg")
return 'tyw'
print(h.__name__)#inner
View Code
wraps
from functools import wraps
def wrapper(func): #func = h
@wraps(func)
def inner(*args,**kwargs):
print('在被装饰器之前做的事:')
ret = func(*args,**kwargs)
print('在被装饰的函数执行之后做的事:')
return ret
return inner
@wrapper
def h(day):
print("gg")
return 'tyw'
print(h.__name__)#h
View Code
带参数的装饰器
import time
flag = True
def timmer_out(flag):
def timmer(func):
def inner(*args,**kwargs):
if flag:
start = time.time()
ret = func(*args,**kwargs)
end = time.time()
print(end - start)
return ret
else:
ret = func(*args,**kwargs)
return inner
return timmer
@timmer_out(flag) # == timmer = timmer_out(flag)
# @timmer
def ww():
time.sleep(0.1)
print('hhh')
@timmer_out(flag)
def egt():
time.sleep(0.1)
print('dddgs')
flag = False
ww()
egt()
# hhh
# 0.10020256042480469
# dddgs
# 0.10077428817749023
View Code
多个装饰器装饰一个函数
def wapper1(func):
def inner1():
print('wrap1,bf func')
func()
print('qrp1,af func')
return inner1
def wapper2(func):
def inner2():
print('wrap2,bf func')
func()
print('qrp2,af func')
return inner2
@wapper2
@wapper1
def f():
print('in f')
f()
# wrap2,bf func
# wrap1,bf func
# in f
# qrp1,af func
# qrp2,af func
View Code