<29>python学习笔记——装饰器

    xiaoxiao2021-03-25  115

    装饰器函数——用来对函数的扩展,补充。

    高阶函数+嵌套函数 =》装饰器

    原则:

    1 / 不能修改被装饰的函数的源代码

    2 / 不能修改被装饰的函数的调用方式

    实现装饰器的知识储备:

    1 / 函数即‘变量’

    def test():

          函数体

    -----------------

    test = '函数体'

    2 / 高阶函数

    两个条件:

    1 / 把一个函数名当作实参传给另外一个函数(在不修改被装饰函数源代码的情况下为其添加功能)

    2 / 返回值中包含函数名(不修改函数的调用方式)

    import time def bar(): time.sleep(2) print('in the bar') def test2(func): print(func) return(func) # # print(test2(bar)) bar = test2(bar) bar()

    3 / 嵌套函数

    装饰器的代码示例:

    高阶函数+嵌套函数 =》装饰器

    1 / 不能修改被装饰的函数的源代码

    2 / 不能修改被装饰的函数的调用方式

    由简到繁的代码说明装饰器:

    ——没有传递参数的代码示例讲解:

    import time def timer(func):##装饰器 def deco(): start_time = time.time() func() end_time = time.time() print('the fun run time is %d' %(end_time-start_time)) return deco @timer ##语法糖 等价于test1 = timer(test1) def test1(): time.sleep(2) print('in the test1') test1() ''' 注解: 当test()调用时,实际因为语法糖的功能,等价于test1 = timer(test1),于是调用了装饰器timer, timer传入test1函数作为参数,运行装饰器下面的deco函数,执行deco函数,并return deco,注意这个deco没有小括号,返回的是deco的内存地址值。 最后调用test1()实际上是 “deco内存地址值()”在运行。 ''' ——被装饰函数需要传递参数的代码实力讲解:

    import time def timer(func):##装饰器 def deco(*args,**kwargs):##test2调用的时候传递了参数name,调用装饰器的时候,deco()也需要有参数name ##由于传递进来的参数不好确定,所以用*args,**kwargs接收 start_time = time.time() func(*args,**kwargs) end_time = time.time() print('the fun run time is %d' %(end_time-start_time)) return deco @timer ##语法糖 等价于test1 = timer(test1) def test1(): time.sleep(2) print('in the test1') @timer ##语法糖 等价于test2 = timer(test2) def test2(name): ##test2调用的时候传递了参数name,调用装饰器的时候,deco()也需要有参数name time.sleep(3) print('name:',name) test1() test2('drglon') ''' 注解: 当test2()调用时传入了参数name,实际因为语法糖的功能,等价于test2 = timer(test2(name)),于是调用了装饰器timer,并传入参数 timer传入test2(name)函数作为参数,运行装饰器下面的deco函数,并把参数name传递给deco函数, 执行deco(name)函数,并return deco,注意这个deco没有小括号,返回的是deco的内存地址值。 最后调用test2(name)实际上是 “deco内存地址值(name)”在运行。 因为装饰器中deco()函数需要接收的参数不固定,所以用*args,**kwargs接收。变的通用。 ''' ——关于被装饰函数的返回值如何体现

    import time user,passwd = 'dralon','abc123' def auth(func): def wrapper(*args,**kwargs):##调用的时候有可能传递了参数,调用装饰器的时候,装饰器的函数也需要有参数 username = input('Username:').strip() password = input('Passwd:').strip() if user ==username and passwd == password: print('has authentication') res = func(*args,**kwargs) ##实际上home()的时候,home()的return home返回值需要在这里体现。设置一个变量储存home()函数的return home 返回值 print("这个是个home()的返回值 %s" %res) else: print('please input ') return wrapper def index(): print('welcom to index page') @auth def home(): print('welcom to home page') return home ##这个返回结果,在装饰器中要有返回才能体现出来 @auth def bbs(): print('welcom to bbs page') # index() home() # bbs()

    高阶函数+嵌套函数 =》装饰器
    转载请注明原文地址: https://ju.6miu.com/read-10338.html

    最新回复(0)