Pyhon基础:Monkey Patch(猴子补丁)
2017-08-14 07:34
267 查看
属性在运行时的动态替换,叫做猴子补丁(Monkey Patch)。
1,这个词原来为Guerrilla Patch,杂牌军、游击队,说明这部分不是原装的,在英文里guerilla发音和gorllia(猩猩)相似,再后来就写了monkey(猴子)。
2,还有一种解释是说由于这种方式将原来的代码弄乱了(messing with it),在英文里叫monkeying about(顽皮的),所以叫做Monkey Patch。
猴子补丁的叫法有些莫名其妙,只要和“模块运行时替换的功能”对应就行了。
Monkey patch就是在运行时对已有的代码进行修改,达到hot patch的目的。
Eventlet中大量使用了该技巧,以替换标准库中的组件,比如socket。
首先来看一下最简单的monkey patch的实现。
1
1
2
3
4
5
6
7
8
9
另一个例子引用了,Zope wiki上对Monkey Patch解释:
1
2
3
4
5
6
还有一个比较实用的例子,很多代码用到 import json,后来发现ujson性能更高,如果觉得把每个文件的import json 改成 import ujson as json成本较高,或者说想测试一下用ujson替换json是否符合预期,只需要在入口加上:
这种场景也比较多,比如我们引用团队通用库里的一个模块,又想丰富模块的功能,除了继承之外也可以考虑用Monkey Patch。
个人感觉Monkey Patch带了便利的同时也有搞乱源代码优雅的风险。
为什么叫猴子补丁
属性的运行时替换和猴子也没什么关系,关于猴子补丁的由来网上查到两种说法:1,这个词原来为Guerrilla Patch,杂牌军、游击队,说明这部分不是原装的,在英文里guerilla发音和gorllia(猩猩)相似,再后来就写了monkey(猴子)。
2,还有一种解释是说由于这种方式将原来的代码弄乱了(messing with it),在英文里叫monkeying about(顽皮的),所以叫做Monkey Patch。
猴子补丁的叫法有些莫名其妙,只要和“模块运行时替换的功能”对应就行了。
Monkey patch就是在运行时对已有的代码进行修改,达到hot patch的目的。
Eventlet中大量使用了该技巧,以替换标准库中的组件,比如socket。
首先来看一下最简单的monkey patch的实现。
1
class Foo(object):2
def bar(self):3
print('Foo.bar')4
5
def bar(self):6
print('Modified bar')7
8
Foo().bar()9
Foo.bar = bar10
Foo().bar()11
12
# D:\Python\python\python-3.6.1\Python36-64\python.exe13
# Foo.bar14
# Modified bar由于Python中的名字空间是开放,通过dict来实现,所以很容易就可以达到patch的目的。
1,运行时动态替换模块的方法
stackoverflow上有两个比较热的例子,1
consider a class that has a method get_data.
2
This method does an external lookup (on a database or web API, for example),
3
and various other methods in the class call it. However, in a unit test,
4
you don't want to depend on the external data source - so you dynamically replace the get_data method with a stub that returns some fixed data.
5
6
假设一个类有一个方法get_data。这个方法做一些外部查询(如查询数据库或者Web API等),
7
类里面的很多其他方法都调用了它。
8
然而,在一个单元测试中,你不想依赖外部数据源。所以你用哑方法态替换了这个get_data方法,
9
哑方法只返回一些测试数据。
另一个例子引用了,Zope wiki上对Monkey Patch解释:
1
from SomeOtherProduct.SomeModule import SomeClass
2
3
def speak(self):
4
return "ook ook eee eee eee!"
5
6
SomeClass.speak = speak
还有一个比较实用的例子,很多代码用到 import json,后来发现ujson性能更高,如果觉得把每个文件的import json 改成 import ujson as json成本较高,或者说想测试一下用ujson替换json是否符合预期,只需要在入口加上:
import json import ujson def monkey_patch_json(): json.__name__ = 'ujson' json.dumps = ujson.dumps json.loads = ujson.loads monkey_patch_json()
2,运行时动态增加模块的方法
这种场景也比较多,比如我们引用团队通用库里的一个模块,又想丰富模块的功能,除了继承之外也可以考虑用Monkey Patch。个人感觉Monkey Patch带了便利的同时也有搞乱源代码优雅的风险。
相关文章推荐
- Pyhon基础:(猴子补丁)Monkey Patch
- Pyhon基础:Monkey Patch(猴子补丁)
- Monkey Patch猴子补丁编程方式及其在Ruby中的运用
- python基础===猴子补丁
- 什么是Monkey Patch猴子补丁
- 详解Python编程中对Monkey Patch猴子补丁开发方式的运用
- 什么是猴子补丁(monkey patch)
- 什么是猴子补丁(monkey patch)
- 什么是猴子补丁(monkey patch)
- 详解Python编程中对Monkey Patch猴子补丁开发方式的运用
- 基础入门_Python—Gevent异步/状态获取/超时设置/猴子补丁
- 什么是猴子补丁(monkey patch)
- Ruby使用Monkey Patch猴子补丁方式进行程序开发的示例
- python monkey 猴子补丁技术编程,修改python json dumps方法。
- 第二种方式,修改python unittest的执行顺序,使用猴子补丁
- Pyhon之常用操作符 - 零基础入门学习Python006
- 猴子补丁
- [Effective JavaScript 笔记]第42条:避免使用轻率的猴子补丁
- 猴子补丁 Monkey-patch(重写方法)
- python基础编程_17_猴子吃桃