# 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. 简单易懂的 Vue.js 基础知识 !

    根 vue 实例 let viewModel = new Vue({ // 包含数据.模板.挂载元素.方法.生命周期钩子等选项 }) Hello Wrold  <!-- 这是我们的 View - ...

  2. FC磁盘设备管理

    本文转自:http://www.cactifans.org/linux/1071.html 转自EMC中文支持论坛 – https://community.emc.com/go/chinese   介 ...

  3. 数据库行转列、列转行,pivot透视多列

    这就是典型的行转列问题. 首先说下最简单的思路  用union all select year,sum(m1) m1,sum(m2) m2,sum(m3) m3,sum(m4) m4 from ( s ...

  4. usermod语法

    语法 usermod [-LU][-c <备注>][-d <登入目录>][-e <有效期限>][-f <缓冲天数>][-g <群组>][-G ...

  5. offsetWidth与clientWidth 区别

    offsetWidth       //元素宽度.内边距和边框,不包括外边距 offsetHeight      //元素高度.内边距和边框,不包括外边距 clientWidth        //元 ...

  6. SQL注入之Sqli-labs系列第三十关(基于WAF防护的双引号报错注入)和三十一关

    开始挑战第三十关和三十一关(Protection with WAF) 0x1 前言 这关其实和29关是同样的,login.php页面存在防护,只要检测到存在问题就跳转到hacked.php页面,不同的 ...

  7. spring(三、spring中的eheche缓存、redis使用)

    spring(三.spring中的eheche缓存.redis使用) 本文主要介绍为什么要构建ehcache+redis两级缓存?以及在实战中如何实现?思考如何配置缓存策略更合适?这样的方案可能遗留什 ...

  8. Vagrant 命令详解

    1.查看当前登录的用户系统上所有活动的Vagrant环境的状态. vagrant global-status参数:--prune 清除列表中的无效条目   ... ...    

  9. CCF-模板生成系统-201509-3

    主要是string---STL的运用 趁机整理一下erase, find, substr, replace, insert #include <bits/stdc++.h> using n ...

  10. 《DSP using MATLAB》Problem 7.6

    代码: 子函数ampl_res function [Hr,w,P,L] = ampl_res(h); % % function [Hr,w,P,L] = Ampl_res(h) % Computes ...