day4-装饰器、生成器
1.装饰器(decorator)
################先来看看一个装饰器的例子###################
''' 装饰器(decorator): 定义:本质是函数,(装饰其他函数)就是为其他函数添加附加功能 原则: 1.不能修改被装饰函数的源代码 2.不能修改被装饰函数的调用方式 实现装饰器的知识储备: 1.函数即“变量” 2.高阶函数 a:把一个函数名当作实参,传给另外一个函数(在不修改被装饰函数源代码的情况下,为其添加功能) b:返回值中包含函数名(不修改函数的调用方式) 3.嵌套函数 高阶函数+嵌套函数==>>装饰器 ''' import time #decorator def timer(func): def warpper(): start_time=time.time() func() end_time=time.time() print('this func runtime is: %s' %(end_time-start_time)) return warpper @timer def test1(): time.sleep(1) print('in the test1...') test1()
################下面一步步推出decorator,首先定义一个函数###################
import time def test1(): time.sleep(1) print('in the test1')
################然后给函数附加功能###################
#定义一个高阶函数,给test1附加计算函数运行时间的功能 import time def timmer(func): start_time=time.time() func() end_time=time.time() run_time=end_time-start_time print('the fun is running %s second'%run_time) def test1(): time.sleep(1) print('in the test1') timmer(test1)
---》》虽然附加了功能,但是改变了函数的调用方式
################带返回值的高阶函数,加上嵌套函数###################
#定义一个高阶函数,给test1附加计算函数运行时间的功能 import time # 调用timmer函数时,返回了warrap函数的内存地址 def timmer(func): def warrap(): start_time=time.time() func() end_time=time.time() run_time=end_time-start_time print('the fun is running %s second'%run_time) return warrap def test1(): time.sleep(1) print('in the test1') test1=timmer(test1) #重名替换,这样就做到了不改变原调用方式,然后改函数test1附加了功能 print(test1) test1()
---》》然后使用@timer,就变成示例的装饰器了
################如果原函数有返回值,咋整###################
#定义一个高阶函数,给test1附加计算函数运行时间的功能 import time # 调用timmer函数时,返回了warrap函数的内存地址 def timmer(func): def warrap(): start_time=time.time() func() end_time=time.time() run_time=end_time-start_time print('the fun is running %s second'%run_time) return warrap @timmer def test1(): time.sleep(1) print('in the test1') @timmer def test2(): time.sleep(1) print('in the test2') return 'from the test2' print(test2()) #我擦,返回值没有了,装饰器不好使了 -------------------------打印结果,返回值为none--------------------------- in the test2 the fun is running 1.0008151531219482 second None
################需要如此操作###################
#定义一个高阶函数,给test1附加计算函数运行时间的功能 import time # 调用timmer函数时,返回了warrap函数的内存地址 def timmer(func): def warrap(): start_time=time.time() res=func() #**将func的执行结果赋给一个值** end_time=time.time() run_time=end_time-start_time print('the fun is running %s second'%run_time) return res #**然后返回这个值,这样就做到了返回原函数的返回值** return warrap @timmer def test1(): time.sleep(1) print('in the test1') @timmer def test2(): time.sleep(1) print('in the test2') return 'from the test2' print(test2()) #返回值有了
################如果原函数有参数的话###################
#定义一个高阶函数,给test1附加计算函数运行时间的功能 import time # 调用timmer函数时,返回了warrap函数的内存地址 def timmer(func): def warrap(*args,**kwargs): #原函数带参数 start_time=time.time() res=func(*args,**kwargs) #**将func的执行结果赋给一个值** end_time=time.time() run_time=end_time-start_time print('the fun is running %s second'%run_time) return res #**然后返回这个值,这样就做到了返回原函数的返回值** return warrap @timmer def test1(): time.sleep(1) print('in the test1') @timmer def test2(): time.sleep(1) print('in the test2') return 'from the test2' @timmer def test3(*args,**kwargs): #原函数带有参数 time.sleep(1) print('in the test3') return 'from the test3' print(test3())
################装饰器,终极版###################
''' #decorator终极高潮版,登录验证方式 1.index页面无需验证 2.home页面本地验证 3,bbs页面ldap验证 ''' # def deco(func): # def warrap(*args,**kwargs): # func() # print('this is the decorator') # return warrap # @deco # def test1(): # print('this is test1') # # test1() uname,pwd='albert','abc123' def deco_login(auth_type): def outwarrap(func): def warrap(*args,**kwargs): if auth_type=='local': username=input('username:') password=input('password:') if username==uname and password==pwd: res=func() print('\33[32;1mYou username and password are authentication\33[0m') else: print('\33[31;1mInvalid username or password\33[0m') elif auth_type=='ldap': print('搞毛线ldap') return warrap return outwarrap def index(): print('welcome index page') @deco_login(auth_type='local') def home(): print('welcome home page') @deco_login(auth_type='ldap') def bbs(): print('welcome bbs page') #index() #bbs() home()
2.生成器(generator)