zoukankan      html  css  js  c++  java
  • @method_decorator() 源码解析

     1 # coding:utf-8
     2 """
     3 作用:
     4 原理:闭包 >> 1. 函数内部定义函数
     5              2.内部函数使用外部函数的变量
     6             3.外部函数返回内部函数的引用
     7 带参数的函数装饰器 》》 三层
     8 
     9 类的装饰器 >> 解决self 参数的问题
    10           >> 被装饰的对象是类不是函数
    11 """
    12 
    13 
    14 def method_decorator(decorator, name=''):
    15     print("类装饰器执行了")
    16 
    17     def _dec(cls):
    18         print("真正的类装饰器生成了,并执行了")
    19         is_class = isinstance(cls, type)
    20         if is_class:
    21             method = getattr(cls, name)
    22         else:
    23             raise Exception("该装饰器只用来装饰类")
    24 
    25         def _wrapper(self, *args, **kwargs):
    26             @decorator
    27             def bound_func(*args2, **kwargs2):
    28                 return method(self, *args2, **kwargs2)
    29 
    30             return bound_func(*args, **kwargs)
    31 
    32         setattr(cls, name, _wrapper)
    33         return cls
    34 
    35     return _dec
    36 
    37 def my_decorator(bound_func):
    38     print("函数装饰器执行了")
    39 
    40     def real_func(*args, **kwargs):
    41         print("
    exc decorator code before func
    ")
    42         ret = bound_func(*args, **kwargs)
    43         print("
    exc decorator code after func
    ")
    44         return ret
    45 
    46     return real_func
    47 
    48 """
    49 @method_decorator(decorator=my_decorator, name="get")
    50 ViewTemp = method_decorator(decorator=my_decorator, name="get")(ViewTemp)
    51 ViewTemp = _dec(ViewTemp)
    52 >>> ViewTemp.get = ViewTemp._wrapper 
    53 """
    54 
    55 @method_decorator(decorator=my_decorator, name="get")
    56 class ViewTemp:
    57     def get(self, request):
    58         print("-----this is get method-----", "
    request==", request)
    59 
    60     def post(self, request):
    61         print("-----this is post method----")
    62 
    63 if __name__ == '__main__':
    64     """
    65     ViewTemp().get(request="xixixi)
    66     ViewTemp()._wrapper(request="xixixi)
    67     >>> _wrapper(self, request="xixixi)
    68     >>> >>> @decorator                                      my_decorator(bound_func)
    69     >>> >>> def bound_func(*args2, **kwargs2):              bound_func = real_func
    70                 return method(self, *args2, **kwargs2)
    71             return bound_func(*args, **kwargs)     
    72     >>> real_func(request="xixixi)
    73     >>> >>> print("
    exc decorator code before func
    ") 
    74     >>> >>> ret = bound_func(request="xixixi") >> bound_func(request="xixixi") >> method(self, request="xixixi)
    75     >>> >>> print("
    exc decorator code after func
    ")
    76     >>> >>> return ret
    77     
    78     """
    79     ViewTemp().get(request="xixixi")
    80     pass
  • 相关阅读:
    [管理]管理者应需要知道的一些定律
    推荐IOS Moneky测试工具Fast Monkey
    [原创]强烈推荐Cmd终极替换工具Cmder
    [原创] 2018年测试行业各职位薪水参考表
    [原创]管理者如何激励下属更有效的工作?
    [原创]SonarQube代码质量检查工具介绍
    [原创]F2etest是一个面向前端、测试、产品等岗位的多浏览器兼容性测试整体解决方案。
    [原创] IMB AppScan 9.0下载及破解
    [原创] Influxdb+Grafana+Jmeter性能监控工具搭建
    [原创]浅谈互联网金融接口测试平台搭建
  • 原文地址:https://www.cnblogs.com/lzc978/p/10261113.html
Copyright © 2011-2022 走看看