• 装饰器的应用-装饰器带参数和不带参数


     1 #!/usr/bin/env python
     2 #encoding: utf-8
     3 def start_info():
     4     print ('电视剧开头曲.......')
     5     print ('开始唱歌.......')
     6 def end_info():
     7     print ('电视剧结束曲.......')
     8 
     9 def filter(start_info,end_info): #接收俩函数
    10     def outer(main_fun): #接收装饰的函数
    11         def app(*argv,**kwargs): #接收装饰的函数的参数
    12             print('******************************')
    13             start_info()
    14             main_fun(*argv,**kwargs)#接收装饰的函数的参数
    15             end_info()
    16             print('******************************')
    17         return app
    18     return outer
    19 #先把函数传进去,然后在用里面的函数装饰
    20 #传函数的装饰器必须有三个def ,第一个是接受函数,第二个是装饰函数的,返回第三个函数对象
    21 # 把需要装饰的函数重新定义,然后调用调用
    22 #1: filter(start_info,end_info):
    23 #2: @outer ->  one_info = outer(one_info)
    24 @filter(start_info,end_info) #这里传入俩个函数
    25 def one_info(name,info):
    26     print ('this is one')
    27     print('wolcome to tv %s .......' % (name))
    28     print('wolcome to tv %s .......' % (info))
    29 
    30 @filter(start_info,end_info)
    31 def two_info(name,info):
    32     print('this is two')
    33     print('wolcome to tv %s .......' % (name))
    34     print('wolcome to tv %s .......' % (info))
    35 
    36 @filter(start_info,end_info)
    37 def three_info(name,info):
    38     print('this is three')
    39     print('wolcome to tv %s .......' % (name))
    40     print('wolcome to tv %s .......' % (info))
    41 
    42 if __name__ == "__main__":
    43     print('三国演义三部曲开始。。。。。。。。')
    44     print('第一部。。。。。。。。。。。。。。')
    45     one_info('三国电视剧第一部', '三国大战')
    46 
    47     print('第二部。。。。。。。。。。。。。。')
    48     two_info('三国电视剧第二部', '三国英雄')
    49 
    50     print('第三部。。。。。。。。。。。。。。')
    51     three_info('三国电视剧第三部', '三国鼎力')
     1 #!/usr/bin/env python
     2 #encoding: utf-8
     3 def start_info():
     4     print ('电视剧开头曲.......')
     5     print ('开始唱歌.......')
     6 def end_info():
     7     print ('电视剧结束曲.......')
     8 
     9 def outer(main_fun): #接收装饰的函数
    10     def app(*argv,**kwargs): #接收装饰的函数的参数
    11         print('******************************')
    12         start_info()
    13         main_fun(*argv,**kwargs)#接收装饰的函数的参数
    14         end_info()
    15         print('******************************')
    16     return app
    17 
    18 #1: @outer ->  one_info = outer(one_info)
    19 @outer
    20 def one_info(name,info):
    21     print ('this is one')
    22     print('wolcome to tv %s .......' % (name))
    23     print('wolcome to tv %s .......' % (info))
    24 
    25 @outer
    26 def two_info(name,info):
    27     print('this is two')
    28     print('wolcome to tv %s .......' % (name))
    29     print('wolcome to tv %s .......' % (info))
    30 
    31 @outer
    32 def three_info(name,info):
    33     print('this is three')
    34     print('wolcome to tv %s .......' % (name))
    35     print('wolcome to tv %s .......' % (info))
    36 
    37 if __name__ == "__main__":
    38     print ('三国演义三部曲开始。。。。。。。。')
    39     print ('第一部。。。。。。。。。。。。。。')
    40     one_info('三国电视剧第一部','三国大战')
    41 
    42     print('第二部。。。。。。。。。。。。。。')
    43     two_info('三国电视剧第二部','三国英雄')
    44 
    45     print('第三部。。。。。。。。。。。。。。')
    46     three_info('三国电视剧第三部','三国鼎力')
  • 相关阅读:
    【转】QT QTableWidget 用法总结
    【转】OpenGL光照处理
    VTK 使用笔记
    【转】toggled 和 trigger 的区别
    白噪声 高斯白噪声
    离线 截图
    【转】 什么是句柄?为什么会有句柄?HANDLE
    VS 常用快捷键 整理
    【转】OpenCV 编程入门
    "This qt version uses an unsupported makefile generator (used: ,supported: MSVC.NET, MSBUILD)"
  • 原文地址:https://www.cnblogs.com/liuyoushui/p/6013096.html
Copyright © 2020-2023  润新知