猴子补丁
2016-08-03 21:22
183 查看
A MonkeyPatch is a piece of Python code which extends or modifies other code at runtime (typically at startup).
A simple example looks like this:
from SomeOtherProduct.SomeModule import SomeClass def speak(self): return "ook ook eee eee eee!" SomeClass.speak = speak
For instance, consider a class that has a method
get_data. This method does an external lookup (on a database or web API, for example), and various other methods in the class call it.
However, in a unit test, you don't want to depend on the external data source - so you dynamically replace the
get_datamethod with a stub that returns some fixed data.
Because Python classes are mutable, and methods are just attributes of the class, you can do this as much as you like - and, in fact, you can even replace classes and functions in a module in exactly the same way.
相关文章推荐
- 原型污染和猴子补丁 Prototype Pollution and Monkey-Patching,monkeypatching
- JS 原型污染和猴子补丁
- 详解Python编程中对Monkey Patch猴子补丁开发方式的运用
- python基础===猴子补丁
- 什么是Monkey Patch猴子补丁
- python monkey 猴子补丁技术编程,修改python json dumps方法。
- 什么是猴子补丁(monkey patch)
- 基础入门_Python-模块和包.Gevent异步/状态获取/超时设置/猴子补丁?
- 什么是猴子补丁(monkey patch)
- [Effective JavaScript 笔记]第42条:避免使用轻率的猴子补丁
- 猴子补丁
- 基础入门_Python—Gevent异步/状态获取/超时设置/猴子补丁
- python 模块会导入几次?猴子补丁为什么可以实现?
- 第二种方式,修改python unittest的执行顺序,使用猴子补丁
- 什么是猴子补丁(monkey patch)
- Python 猴子补丁
- python设计模式之猴子补丁模式
- Ruby使用Monkey Patch猴子补丁方式进行程序开发的示例
- Monkey Patch猴子补丁编程方式及其在Ruby中的运用
- Pyhon基础:(猴子补丁)Monkey Patch