python猴子补丁是什么_什么是Python的monkeypatch(猴子补丁)?
這個問題是有一次面試的時候問到的,當時確實第一次聽說這個,單純只是非常好奇,于是就去了解了一下。
在維基百科上找到了:我理解的就是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平臺的插件分發)。
總結
以上是生活随笔為你收集整理的python猴子补丁是什么_什么是Python的monkeypatch(猴子补丁)?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: dbforge schema compa
- 下一篇: kademlia java_分布式哈希表