python中的猴子补丁Monkey Patch
python中的猴子补丁Monkey Patch
什么是猴子补丁
the term monkey patch only refers to dynamic modifications of a class or module at runtime, motivated by the intent to patch existing third-party code as a workaround to a bug or feature which does not act as desired
即在运行时对方法 / 类 / 属性 / 功能进行修改,把新的代码作为解决方案代替原有的程序,也就是为其打上补丁。
为什么叫做猴子补丁
The term monkey patch seems to have come from an earlier term, guerrilla patch, which referred to changing code sneakily – and possibly incompatibly with other such patches – at runtime.The word guerrilla, homophonous with gorilla (or nearly so), became monkey, possibly to make the patch sound less intimidating.[1] An alternative etymology is that it refers to “monkeying about” with the code (messing with it).
- 一种说法杂牌军、游击队的英文发音与猩猩相似,杂牌军、游击队不是原装军队,就像是替补,所以也就演变叫做猴子补丁
- 另一种说法“monkeying about”有胡闹,顽皮,哄骗的意思,所以叫做猴子补丁
python中使用猴子补丁
class Example():
def func1(self):
print('我才是原装')
def func2(*args):
print('我要取代你')
def func3(*args):
print('都给我一边去')
instance = Example()
Example.func1 = func2
instance.func1() # 我要取代你
instance.func1 = func3
instance.func1() # 都给我一边去
instance2 = Example()
instance2.func1() # 我要取代你
例子非常简单,func2取代的是类的方法,func3取代的是实例的方法,最终输出都不是原装
其他例子
在使用gevent模块的使用就会遇到猴子补丁
import gevent.monkey
gevent.monkey.patch_all()
使用猴子补丁的方式,gevent能够修改标准库里面大部分的阻塞式系统调用,包括socket、ssl、threading和 select等模块,而变为协作式运行。也就是通过猴子补丁的monkey.patch_xxx()来将python标准库中模块或函数改成gevent中的响应的具有协程的协作式对象。这样在不改变原有代码的情况下,将应用的阻塞式方法,变成协程式的。
这里参考https://blog.csdn.net/wangjianno2/article/details/51708658
注意问题
在使用猴子补丁的时候同样容易出现问题
- 当进行版本更新变化的时候,很容易对补丁做出破坏
- 不知情的情况下对一个位置打两个补丁会造成替换
- 对于不知道有补丁的人来说可能会对出现的某些情况感到困惑
参考https://en.wikipedia.org/wiki/Monkey_patch
python中的猴子补丁Monkey Patch的更多相关文章
- python的猴子补丁monkey patch
monkey patch指的是在运行时动态替换,一般是在startup的时候. 用过gevent就会知道,会在最开头的地方gevent.monkey.patch_all();把标准库中的thread/ ...
- 什么是猴子补丁(monkey patch)
monkey patch指的是在执行时动态替换,通常是在startup的时候. 用过gevent就会知道,会在最开头的地方gevent.monkey.patch_all();把标准库中的thread/ ...
- Python中的猴子补丁
monkey patch指的是在运行时动态替换,一般是在startup的时候.用过gevent就会知道,会在最开头的地方gevent.monkey.patch_all();把标准库中的thread/s ...
- python设计模式之猴子补丁模式
1.所有书中都没有把猴子补丁作为一种设计模式来看待.因为设计模式的模式的命名是根据java中提炼出来的,语言方式决定了java绝对不会有也不需要有这种操作,不存在的.那自然设计模式不会包括猴子补丁模式 ...
- 猴子补丁(Monkey Patching)
猴子补丁是我在面试的时候接触的一到题,学python的时候,我根本就没有听说这个概念!那接下来我们来分析一下: 1.什么是猴子补丁? 2.猴子补丁的功能? 3.猴子补丁的应用场景? 一.什么是猴子补丁 ...
- Gevent和猴子补丁
定义 在2018年看Flutent python时了解到猴子补丁,知道咋回事,但是现在通过代码更深刻认识猴子补丁. 猴子补丁:在运行时修改类或模块,而不改动源码. 例子1 没有用猴子补丁 import ...
- Python Monkey patch猴子补丁
monkey patch (猴子补丁) 用来在运行时动态修改已有的代码,而不需要修改原始代码. 简单的monkey patch 实现:[python] #coding=utf-8 def orig ...
- Python 中的鸭子类型和猴子补丁
原文链接: Python 中的鸭子类型和猴子补丁 大家好,我是老王. Python 开发者可能都听说过鸭子类型和猴子补丁这两个词,即使没听过,也大概率写过相关的代码,只不过并不了解其背后的技术要点是这 ...
- python monkey 猴子补丁技术编程,修改python json dumps方法。
1.猴子补丁就是不改变原有模块的内容的前提下,给原有模块新增方法或者修改原有模块. 一个模块的函数,如果希望改变函数的功能,不改变函数名,通常是库模块,你不可能去修改三方库的源码的,实施起来不方便,而 ...
随机推荐
- Go语言学习笔记(四) [array、slice、map]
日期:2014年7月22日 一.array[数组] 1.定义:array 由 [n]<type> 定义,n 标示 array 的长度,而 <type> 标示希望存储的内 ...
- Linux上磁盘热插拔
首先获取scsi设备的信息. [root@server2 ~]# lsscsi [:::] disk VMware, VMware Virtual S 1.0 /dev/sda [:::] cd/dv ...
- Netty 粘包 & 拆包 & 编码 & 解码 & 序列化 介绍
目录: 粘包 & 拆包及解决方案 ByteToMessageDecoder 基于长度编解码器 基于分割符的编解码器 google 的 Protobuf 序列化介绍 其他的 前言 Netty 作 ...
- 第一册:lesson sixty seven。
原文: The weekend. A:Hello , were you an tht butcher's? B:Yes I was. A:Were you at the butcher's too? ...
- tomcat域名绑定设置
域名绑定分为单域名绑定.多域名绑定,配置主要涉及到tomcat目录下conf/server.xml文件 一.单域名绑定 1.修改server.xml 大约105行的内容(不是必须修改,如果只是绑定一个 ...
- C#.NET和C++结构体Socket通信与数据转换
最近在用C#做一个项目的时候,Socket发送消息的时候遇到了服务端需要接收C++结构体的二进制数据流,这个时候就需要用C#仿照C++的结 构体做出一个结构来,然后将其转换成二进制流进行发送,之后将响 ...
- SpringMVC表单验证与Velocity整合
阅读本文约“1.2分钟” 定义表单类 以Login为例,有username和password两个字段 import javax.validation.constraints.NotNull; impo ...
- laravel前后台路由分离
在laravel中创建文件放置前台和后台控制器 找到app/providers/RouteServiceProvider.PHP文件 在内配置 例: <?php namespace App\Pr ...
- laravel接值 get post
laravel使用一种简单的方式来访问用户提交的信息. 你可以用统一的方式来访问用户提交的信息,而不用为用户提交信息的方式操心. 引用类:use Illuminate\Support\Facades\ ...
- jQuery 事件 - ready() 方法
转载:http://www.w3school.com.cn/jquery/jquery_hide_show.asp 实例 在文档加载后激活函数: $(document).ready(function( ...