什么是猴子补丁
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
注重题目
在运用猴子补丁的时刻一样轻易涌现题目
当举行版本更新变化的时刻,很轻易对补丁做出损坏不知情的状况下对一个位置打两个补丁会形成替代关于不知道有补丁的人来讲能够会对涌现的某些状况觉得疑心
以上就是python中猴子补丁是什么?怎样用?的细致内容,更多请关注ki4网别的相干文章!