# coding:utf-8
"""
作用:
原理:闭包 >> 1. 函数内部定义函数
2.内部函数使用外部函数的变量
3.外部函数返回内部函数的引用
带参数的函数装饰器 》》 三层 类的装饰器 >> 解决self 参数的问题
>> 被装饰的对象是类不是函数
""" def method_decorator(decorator, name=''):
print("类装饰器执行了") def _dec(cls):
print("真正的类装饰器生成了,并执行了")
is_class = isinstance(cls, type)
if is_class:
method = getattr(cls, name)
else:
raise Exception("该装饰器只用来装饰类") def _wrapper(self, *args, **kwargs):
@decorator
def bound_func(*args2, **kwargs2):
return method(self, *args2, **kwargs2) return bound_func(*args, **kwargs) setattr(cls, name, _wrapper)
return cls return _dec def my_decorator(bound_func):
print("函数装饰器执行了") def real_func(*args, **kwargs):
print("\nexc decorator code before func\n")
ret = bound_func(*args, **kwargs)
print("\nexc decorator code after func\n")
return ret return real_func """
@method_decorator(decorator=my_decorator, name="get")
ViewTemp = method_decorator(decorator=my_decorator, name="get")(ViewTemp)
ViewTemp = _dec(ViewTemp)
>>> ViewTemp.get = ViewTemp._wrapper
""" @method_decorator(decorator=my_decorator, name="get")
class ViewTemp:
def get(self, request):
print("-----this is get method-----", "\nrequest==", request) def post(self, request):
print("-----this is post method----") if __name__ == '__main__':
"""
ViewTemp().get(request="xixixi)
ViewTemp()._wrapper(request="xixixi)
>>> _wrapper(self, request="xixixi)
>>> >>> @decorator my_decorator(bound_func)
>>> >>> def bound_func(*args2, **kwargs2): bound_func = real_func
return method(self, *args2, **kwargs2)
return bound_func(*args, **kwargs)
>>> real_func(request="xixixi)
>>> >>> print("\nexc decorator code before func\n")
>>> >>> ret = bound_func(request="xixixi") >> bound_func(request="xixixi") >> method(self, request="xixixi)
>>> >>> print("\nexc decorator code after func\n")
>>> >>> return ret """
ViewTemp().get(request="xixixi")
pass

@method_decorator() 源码解析的更多相关文章

  1. 【原】Android热更新开源项目Tinker源码解析系列之三:so热更新

    本系列将从以下三个方面对Tinker进行源码解析: Android热更新开源项目Tinker源码解析系列之一:Dex热更新 Android热更新开源项目Tinker源码解析系列之二:资源文件热更新 A ...

  2. 【原】Android热更新开源项目Tinker源码解析系列之一:Dex热更新

    [原]Android热更新开源项目Tinker源码解析系列之一:Dex热更新 Tinker是微信的第一个开源项目,主要用于安卓应用bug的热修复和功能的迭代. Tinker github地址:http ...

  3. 【原】Android热更新开源项目Tinker源码解析系列之二:资源文件热更新

    上一篇文章介绍了Dex文件的热更新流程,本文将会分析Tinker中对资源文件的热更新流程. 同Dex,资源文件的热更新同样包括三个部分:资源补丁生成,资源补丁合成及资源补丁加载. 本系列将从以下三个方 ...

  4. 多线程爬坑之路-Thread和Runable源码解析之基本方法的运用实例

    前面的文章:多线程爬坑之路-学习多线程需要来了解哪些东西?(concurrent并发包的数据结构和线程池,Locks锁,Atomic原子类) 多线程爬坑之路-Thread和Runable源码解析 前面 ...

  5. jQuery2.x源码解析(缓存篇)

    jQuery2.x源码解析(构建篇) jQuery2.x源码解析(设计篇) jQuery2.x源码解析(回调篇) jQuery2.x源码解析(缓存篇) 缓存是jQuery中的又一核心设计,jQuery ...

  6. Spring IoC源码解析——Bean的创建和初始化

    Spring介绍 Spring(http://spring.io/)是一个轻量级的Java 开发框架,同时也是轻量级的IoC和AOP的容器框架,主要是针对JavaBean的生命周期进行管理的轻量级容器 ...

  7. jQuery2.x源码解析(构建篇)

    jQuery2.x源码解析(构建篇) jQuery2.x源码解析(设计篇) jQuery2.x源码解析(回调篇) jQuery2.x源码解析(缓存篇) 笔者阅读了园友艾伦 Aaron的系列博客< ...

  8. jQuery2.x源码解析(设计篇)

    jQuery2.x源码解析(构建篇) jQuery2.x源码解析(设计篇) jQuery2.x源码解析(回调篇) jQuery2.x源码解析(缓存篇) 这一篇笔者主要以设计的角度探索jQuery的源代 ...

  9. jQuery2.x源码解析(回调篇)

    jQuery2.x源码解析(构建篇) jQuery2.x源码解析(设计篇) jQuery2.x源码解析(回调篇) jQuery2.x源码解析(缓存篇) 通过艾伦的博客,我们能看出,jQuery的pro ...

随机推荐

  1. less 查看日志

    上下键,空格下一页,字母B上一页  ,字母Q退出,n(小写字母n)上一个关键字位置N(大写字母N)下一个关键词位置] 二.查找某个时间段的日志:比如查询2018-08-15 10:37:32到2018 ...

  2. jquery mCustomScrollbar 滚动条宽度的设置

    一.项目使用 $("#iscroll-1, #tree_box, .work, .item1, .item2, .item3, .item4").mCustomScrollbar( ...

  3. 2017年4月7日16:18:17 java8 常用记录

    List<String> customerUids = customerTagModel.stream().map(CustomerTagModel::getCustomerUid)   ...

  4. $inject

    function breadcrumb($parse, store) { } // 为了压缩 breadcrumb.$inject = ['$parse', 'breadcrumbStore']

  5. Window系统下搭建GIT本地服务器

    转载:https://blog.csdn.net/qwer971211/article/details/71156055

  6. hdu5000 Clone dp+大数

    After eating food from Chernobyl, DRD got a super power: he could clone himself right now! He used t ...

  7. python中时间、日期、时间戳的转换

    1.简介 在编写代码时,往往涉及时间.日期.时间戳的相互转换. 2.示例 # 引入模块 import time, datetime 2.1 str类型的日期转换为时间戳 # 字符类型的时间 tss1 ...

  8. 好久没考虑过的 sql 注入

    很多年没考虑 sql 注入了,毕业以后 使用mybatis #{ 参数的 语法 },这个 语法已经 做了防止 sql 注入的处理 . 看到同事写的 ${ 参数 },突然 想到这个问题 . 下面聊聊 s ...

  9. LOJ 2292 「THUSC 2016」成绩单——区间DP

    题目:https://loj.ac/problem/2292 直接 DP 很难做,主要是有那种 “一个区间内部有很多个别的区间” 的情况. 自己想了一番枚举 max-min 的最大限制,然后在该基础上 ...

  10. spring @Autowired注入对象,在构造方法中为null问题

    出现问题的代码如下: @Service public class BaseHttpServiceImpl implements BaseHttpClient { private final stati ...