• @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
  • 相关阅读:
    2.GO-可变参数函数、匿名函数和函数变量
    1.Go-copy函数、sort排序、双向链表、list操作和双向循环链表
    第四章、Go-面向“对象”
    第三章、Go-内建容器
    第二章、Go-基础语法
    第一章、Go安装与Goland破解
    arthas使用分享
    redis如何后台启动
    java.io.IOException: Could not locate executable nullinwinutils.exe in the Hadoop binaries
    安装redis出现cc adlist.o /bin/sh:1:cc:not found的解决方法
  • 原文地址:https://www.cnblogs.com/lzc978/p/10261113.html
Copyright © 2020-2023  润新知