@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 ...
随机推荐
- 远程连接Linux
远程连接Linux 为什么要远程连接Linux 在实际的工作场景中,虚拟机界面或者物理服务器本地的终端都是很少接触的,因为服务器装完系统之后,都要拉倒IDC机房托管,如果是购买的云主机,那更碰不到 ...
- mac 常用终端命令
mkdir 文件夹夹名称 创建一个文件夹 cd 文件夹名称 进入该文件夹 git init 在该文件夹下创建一个git仓库 touch 文件名称 在该文件夹下创建一个文件 echo '内容' &g ...
- ui学习心得
软工课上我分配的任务是界面设计,但是对于初期一个ps从没接触的人来说我觉得还是有点困难,所以要从头学起. PS的功能是什么呢?对于业余爱好者来说,主要还是做一些简单的处理照片.绘图.设计一些皮肤等, ...
- elasticsearch 的查询 /_nodes/stats 各字段意思
/_nodes/stats 字段意思 “” 1 { 2 "_nodes": {3 "total": 1, "successful" ...
- apk签名的流程
最后总结一下apk签名的整个流程: 一.对Apk中的每个文件做一次算法(数据SHA1摘要+Base64编码),保存到MANIFEST.MF文件中 二.对MANIFEST.MF整个文件做一次算法(数据S ...
- leetcode 3.Longest Substring Without Repeating Charcters
在一个字符串中寻找出最长的无重复字符的子串的,在不断的后续检索中需要去掉前面一个重复的字符,那么就是需要记录之前所出现过的字符的,在这里需要利用hashmap来记录字符和其出现的位置之间的映射关系的, ...
- Linux装agent
解压Linux.zip Linux的负载机链接:https://pan.baidu.com/s/1yrmsT3PYfuL9Wlh4FEYxaA 密码:s72n unzip Linux.zip chm ...
- arch 安装准备--包管理的使用pacman
-------https://wiki.archlinux.org/index.php/Pacman/Tips_and_tricks#List_of_installed_packageshttps:/ ...
- js生成的验证码
例1 <!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title&g ...
- 备忘:EBS参考链接
帮助手册查阅网址http://docs.oracle.com/cd/V46499_01/current/html/docset.html 官方 http://www.oracleappshub.co ...