---------------------函数的嵌套------ l=[1,2] l.__iter__()#iter(l) 装饰器:本质就是函数的修饰作用 原则: 1.不修改被修饰函数的源代码 2.不修改被修饰函数的调用方式 import time l=[1,3] def cal(l): res=0 start_time=time.time() for i in l: time.sleep(0.1) res+=i stop_time=time.time() print('函数的运行时间是:%s'%(stop_time-start_time)) return res start=cal(l) 装饰器=高阶函数+函数嵌套+闭包 高阶函数: 1.函数的接受参数是一个函数名称 2.函数的返回值是一个函数名称 3.满足上面一个函数就是高阶函数 import time def foo(): print('你好啊') time.sleep(2) def test(func): print(func) start_time=time.time() func() stop_time=time.time() print('函数的运行时间:%s'%(stop_time-start_time)) test(foo) def foo(): print('from foo') def test(): func=foo() return func foo=test() 多运行一次 #高阶函数 def foo(): time.sleep(3) print('来之foo') def timer(func): start_time=time.time() func() stop_time=time.time() print('函数的运行时间是:%s'%(stop_time-start_time)) return func foo=timer(foo) foo() 函数嵌套 def father(name): print('from father %s'%name) def soon(): print('from soon') print(locals()) father('alex') #闭包其实就是嵌套来执行 import time @timmer def timmer(func): def wrapper(): start_time=time.time() print(func) func() stop_time=time.time() print("函数的运行时间是:%s"%(stop_time-start_time)) return wrapper def test(): time.sleep(2) print("test函数执行完毕") #test=timer(test) test() #@符号就是语法糖 import time def timmer(func): #func=test def wrapper(): # print(func) start_time=time.time() res=func() #就是在运行test() stop_time = time.time() print('运行时间是%s' %(stop_time-start_time)) return res return wrapper @timmer #test=timmer(test) def test(): time.sleep(3) print('test函数运行完毕') res=test() print(res) -------------装饰器实例-------------- import time def timmer(func): def wapper(*args,**kwargs): start_time=time.time() stop_time=time.time() res=func(*args,**kwargs) print('函数的运行时间是:%s'%(stop_time-start_time)) return res return wapper() @timmer def cal(l): res=0 for i in l: time.sleep(0.1) res+=i return res res=cal(range(10)) print(res) ==================加上返回值装饰器========== import time def timmer(func): #func=test def wrapper(): # print(func) start_time=time.time() res=func() #就是在运行test() stop_time = time.time() print('运行时间是%s' %(stop_time-start_time)) return res return wrapper @timmer #test=timmer(test) def test(): time.sleep(3) print('test函数运行完毕') res=test() print(res) ====================加上参数装饰器========= import time def timmer(func): #func=test def wrapper(*args,**kwargs): # print(func) start_time=time.time() res=func(*args,*kwargs) #就是在运行test() stop_time = time.time() print('运行时间是%s' %(stop_time-start_time)) return res return wrapper @timmer #test=timmer(test) def test(name,age): time.sleep(3) print('test的函数运行完毕,名字是【%s】,年龄是【%s】'%(name,age)) print('test函数运行完毕') return '这是test的返回值' @timmer def test1(name,age,gende): time.sleep(2) print('test1的函数运行完毕,名字是【%s】,年龄是【%s】,性别是:【%s】'%(name,age,gende)) print('test1函数运行完毕') return '这是test1的返回值' res=test('lignhaifeng',18) res1=test1('ouyang','18','boy') print(res) print(res1) 解压序列进行取包 sl=[1,2,3,4,54,66,76,8768,89786,989,906,90,90987,988,9889] a,b,c,*_,d=sl#中间不需要的值用*_代替 print(a,b,c,d) a=1 b=2 a,b=b,a print(a,b) =====================装饰器验证=================== import os user_dic={'username':None,'login':False} user_list=[ {'name':'alex','passwd':'123'}, {'name':'linhaifeng','passwd':'123'}, {'name':'wupeiqi','passwd':'123'}, {'name':'yuanhao','passwd':'123'}, ] current_dic={'username':None,'login':False} def auth(auth_type='filedb'): def auth_func(func): def wrapper(*args,**kwargs): print("认证的类型是",auth_type) if auth_type=='filedb': if current_dic['username'] and current_dic['login']: res = func(*args, **kwargs) return res username=input('用户名:').strip() passwd=input('密码:').strip() for user_dic in user_list: if username == user_dic['name'] and passwd == user_dic['passwd']: current_dic['username']=username current_dic['login']=True res = func(*args, **kwargs) return res else: print('用户名或者密码错误') elif auth_type=='ldap': print('鬼才会玩这个') return wrapper return auth_func @auth(auth_type='filedb') def index(): print('欢迎来到京东主页') @auth(auth_type='filedb') def home(name): print('欢迎回家%s' %name) @auth(auth_type='filedb') def shopping_car(name): print('%s的购物车里有[%s,%s,%s]' %(name,'奶茶','妹妹','娃娃')) print('before-->',current_dic) index() print('after--->',current_dic) home('产品经理') #shopping_car('产品经理')
day20函数的装饰器和闭包
猜你喜欢
转载自blog.csdn.net/qq_37311616/article/details/80545544
今日推荐
周排行