这个问题是有一次面试的时候问到的,当时确实第一次听说这个,单纯只是非常好奇,于是就去了解了一下。
在维基百科上找到了:我理解的就是Python的一种运行时的机制,简单来说就是这样一个效果。
>>> import math
>>> math.pi
3.141592653589793
>>> math.pi = 1
>>> math.pi
1
猴子补丁就是在运行过程中动态修改一个类的方法或变量等。monkey patching is used to:
Replace methods / classes / attributes / functions at runtime, e.g. to stub out a function during testing;
Modify/extend behaviour of a third-party product without maintaining a private copy of the source code;
Apply the result of a patch at runtime to the state in memory, instead of the source code on disk;
Distribute security or behavioural fixes that live alongside the original source code (an example of this would be distributing the fix as a plugin for the Ruby on Rails platform).
猴子补丁应用于:
在运行阶段替换方法/类别/属性/功能。
修改/扩展第三方产品的行为而不需要维护源代码的私有拷贝;
将运行时补丁的结果应用于内存中的状态,而不是磁盘上的源代码;
分发与原始源代码共存的安全性或行为修复程序(例如,将修复程序作为Ruby on Rails平台的插件分发)。