@method_decorator() 源码解析
# 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() 源码解析的更多相关文章
- 【原】Android热更新开源项目Tinker源码解析系列之三:so热更新
本系列将从以下三个方面对Tinker进行源码解析: Android热更新开源项目Tinker源码解析系列之一:Dex热更新 Android热更新开源项目Tinker源码解析系列之二:资源文件热更新 A ...
- 【原】Android热更新开源项目Tinker源码解析系列之一:Dex热更新
[原]Android热更新开源项目Tinker源码解析系列之一:Dex热更新 Tinker是微信的第一个开源项目,主要用于安卓应用bug的热修复和功能的迭代. Tinker github地址:http ...
- 【原】Android热更新开源项目Tinker源码解析系列之二:资源文件热更新
上一篇文章介绍了Dex文件的热更新流程,本文将会分析Tinker中对资源文件的热更新流程. 同Dex,资源文件的热更新同样包括三个部分:资源补丁生成,资源补丁合成及资源补丁加载. 本系列将从以下三个方 ...
- 多线程爬坑之路-Thread和Runable源码解析之基本方法的运用实例
前面的文章:多线程爬坑之路-学习多线程需要来了解哪些东西?(concurrent并发包的数据结构和线程池,Locks锁,Atomic原子类) 多线程爬坑之路-Thread和Runable源码解析 前面 ...
- jQuery2.x源码解析(缓存篇)
jQuery2.x源码解析(构建篇) jQuery2.x源码解析(设计篇) jQuery2.x源码解析(回调篇) jQuery2.x源码解析(缓存篇) 缓存是jQuery中的又一核心设计,jQuery ...
- Spring IoC源码解析——Bean的创建和初始化
Spring介绍 Spring(http://spring.io/)是一个轻量级的Java 开发框架,同时也是轻量级的IoC和AOP的容器框架,主要是针对JavaBean的生命周期进行管理的轻量级容器 ...
- jQuery2.x源码解析(构建篇)
jQuery2.x源码解析(构建篇) jQuery2.x源码解析(设计篇) jQuery2.x源码解析(回调篇) jQuery2.x源码解析(缓存篇) 笔者阅读了园友艾伦 Aaron的系列博客< ...
- jQuery2.x源码解析(设计篇)
jQuery2.x源码解析(构建篇) jQuery2.x源码解析(设计篇) jQuery2.x源码解析(回调篇) jQuery2.x源码解析(缓存篇) 这一篇笔者主要以设计的角度探索jQuery的源代 ...
- jQuery2.x源码解析(回调篇)
jQuery2.x源码解析(构建篇) jQuery2.x源码解析(设计篇) jQuery2.x源码解析(回调篇) jQuery2.x源码解析(缓存篇) 通过艾伦的博客,我们能看出,jQuery的pro ...
随机推荐
- python文件读写,以后就用with open语句
读写文件是最常见的IO操作.Python内置了读写文件的函数,用法和C是兼容的. 读写文件前,我们先必须了解一下,在磁盘上读写文件的功能都是由操作系统提供的,现代操作系统不允许普通的程序直接操作磁盘, ...
- 截取URL链接中字段的方法
第一个页面像第二个页面传参方法 location.href = "poster.html?" + "name=" + name + "&tim ...
- 2019.4 sigfox EMC
干扰源: ------- Leakage Sensor 有-30dB的谐波 1在NPN 基级加100pF 电容 从VCC到GND,一级级整改.
- MySQL:视图
视图 一.视图的概述 1. 含义:是从数据库中一个或多个表中导出的虚拟表2. 作用:①简单化 ②安全性 ③逻辑数据独立性3. 注意:一个表可以由多个视图: 二.视图的创建 1. 总的语法形式 CREA ...
- C语言gcc处理过程
gcc编译C文件一共四步,预处理(Preprocess),编译(Compilation),汇编(Assembly)和链接(Linking) 1. 预处理(Preprocess) 预处理是预处理中会展开 ...
- Windows10 VS2017 C++ Json解析(使用jsoncpp库)
1.项目必须是win32 2.生成的lib_json.lib放到工程目录下 3.incldue的头文件放到工程目录,然后设置工程->属性->配置属性->vc++目录->包含目录 ...
- 德州扑克AI--Programming Poker AI(译)
前言: 最近在研究德州扑克的AI, 也想由浅入深的看下, 在网上找了一圈, 发现很多文章都提到了一篇文章: Programming Poker AI. 仔细拜读了一下, 觉得非常不错. 这里作下简单的 ...
- JAVA常用设计模式(一、单例模式、工厂模式)
JAVA设计模式之单例模式 import java.util.HashMap; import java.util.Map; /** * 设计模式之单例模式 * 单例模式(Singleton Patte ...
- Windows7下安装python3.6.3
官网下载即可!看清自己的电脑是32还是64! 分享一个网速慢同学可下载网址:https://pan.baidu.com/s/1kU5OCOB#list/path=%2Fpub%2Fpython 1.p ...
- Centos7 安装nginx1.14
一丶官网 http://nginx.org/en/download.html 至于安装那个版本首先要看清楚版本代表什么意思 Nginx官网提供了三个类型的版本Mainline version:Main ...