What is monkey patching?

前端 未结 8 2034
[愿得一人]
[愿得一人] 2020-11-21 16:35

I am trying to understand, what is monkey patching or a monkey patch?

Is that something like methods/operators overloading or delegating?

Does it have anyt

相关标签:
8条回答
  • 2020-11-21 17:20

    Monkey patching is reopening the existing classes or methods in class at runtime and changing the behavior, which should be used cautiously, or you should use it only when you really need to.

    As Python is a dynamic programming language, Classes are mutable so you can reopen them and modify or even replace them.

    0 讨论(0)
  • 2020-11-21 17:21

    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
    

    Source: MonkeyPatch page on Zope wiki.

    0 讨论(0)
提交回复
热议问题