• python学习day12 装饰器进阶


    # 装饰器的进阶
    # functools.wraps
    # 带参数的装饰器
    # 多个装饰器装饰同一个函数
    
    # 装饰器
    # 开发原则 : 开放封闭原则
    # 装饰器的作用 :在不改变原函数的调用方式的情况下,在函数的前后添加功能
    # 装饰器的本质 : 闭包函数
    
    # def wrapper(func):
    #     def inner(*args,**kwargs):
    #         print('在被装饰的函数执行之前做的事')
    #         ret = func(*args,**kwargs)
    #         print('在被装饰的函数执行之后做的事')
    #         return ret
    #     return inner
    #
    # @wrapper   #holiday = wrapper(holiday)
    # def holiday(day):
    #     print('全体放假%s天'%day)
    #     return '好开心'
    #
    # ret = holiday(3)
    # print(ret)
    
    
    # def outer(*args):  #定义是聚合,调用是打散
    #     print(args)
    #     print(*args)
    #     def inner(*args):
    #         print('inner : ',args)
    #     inner(*args)
    # outer(1,2,3,4)
    
    # from functools import wraps
    # def wrapper(func):
    #     @wraps(func)
    #     def inner(*args,**kwargs):
    #         print('之前')
    #         ret = func(*args,**kwargs)
    #         print('之后')
    #         return ret
    #     return inner
    # @wrapper
    # def holiday(day):
    #     '''
    #
    #     :param day:
    #     :return:
    #     '''
    #     print('放假%s天'%day)
    #     return 'happly'
    # holiday(1)
    # # print(holiday.__name__) #holiday其实是指向inner
    # # print(holiday.__doc__)  #显示注释
    # print(holiday.__name__)    #引入了函数waper就可以以原名字调用原函数
    # print(holiday.__doc__)
    
    # 带参数的装饰器(就是多了一个可以控制装饰器的函数嵌套)
    #就是控制装饰器是否工作
    
    # import time
    # from functools import wraps
    # FLAG = False
    # def timmer_out(flag):
    #     def timmer(func):
    #         @wraps(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 ret
    #         return inner
    #     return timmer
    #
    # @timmer_out(FLAG)
    # def wahaha():
    #     time.sleep(0.1)
    #     print(wahaha.__name__)
    #
    # @timmer_out(FLAG)
    # def shuangyy():
    #     time.sleep(0.1)
    #     print(shuangyy.__name__)
    #
    # wahaha()
    # shuangyy()
    
    #多个装饰器装饰一个函数
    # def wrapper1(func):
    #     def inner1():
    #         print('wrapper1 ,before func')
    #         ret = func()
    #         print('wrapper1 ,after func')
    #         return ret
    #     return inner1
    #
    # def wrapper2(func):
    #     def inner2():
    #         print('wrapper2 ,before func')
    #         ret = func()
    #         print('wrapper2 ,after func')
    #         return ret
    #     return inner2
    #
    # def wrapper3(func):
    #     def inner3():
    #         print('wrapper3 ,before func')
    #         ret = func()
    #         print('wrapper3 ,after func')
    #         return ret
    #     return inner3
    #
    # @wrapper3
    # @wrapper2
    # @wrapper1
    # def f():
    #     print('in f')
    #     return '哈哈哈'
    #
    # print(f())
  • 相关阅读:
    【Docker 命令】- ps命令
    【Docker 命令】- exec命令
    【Docker 命令】- create命令
    【Docker 命令】- pause/unpause 命令
    【Docker 命令】- rm命令
    【Docker 命令】- kill命令
    【Docker 命令】- start/stop/restart命令
    【Docker 命令】- run命令
    【Docker】- 基本命令
    深度学习原理与框架- batch_normalize(归一化操作)
  • 原文地址:https://www.cnblogs.com/wujunjie-sir/p/9195715.html
Copyright © 2020-2023  润新知