Home  >  Article  >  Backend Development  >  What is monkey patching in python? how to use?

What is monkey patching in python? how to use?

不言
不言forward
2019-03-18 09:40:262890browse

The content of this article is about what is the monkey patch in python? how to use? It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

What is monkey patch

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

That is, modify the method/class/attribute/function at runtime and replace the original code with the new code as a solution Some programs are patched.

Why is it called monkey patch

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 ).

One theory is that the English pronunciation of the miscellaneous army and guerrillas is similar to that of orangutans. The miscellaneous army and guerrillas are not the original army, they are just like substitutes, so they are also called monkey patches. Another way The saying "monkeying about" means fooling around, being naughty, or coaxing, so it's called monkey patch

Using monkey patch in 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() # 我要取代你

The example is very simple, func2 replaces the class method, func3 replaces it The method is an example, and the final output is not the original

Other examples

You will encounter monkey patches when using the gevent module

import gevent.monkey
 gevent.monkey.patch_all()

The way to use monkey patches, gevent can modify most of the blocking system calls in the standard library, including socket, ssl, threading and select modules, and change them to cooperative operation. That is, monkeypatch monkey.patch_xxx() is used to change the modules or functions in the Python standard library into responsive collaborative objects with coroutines in gevent. In this way, the application's blocking method can be turned into a coroutine without changing the original code.
Reference here https://blog.csdn.net/wangjianno2/article/details/51708658

Attention issues

It is equally easy when using monkey patches Problems occur

When version updates are made, it is easy to damage the patches. Applying two patches to one location without knowing it will cause replacement. For people who do not know that there are patches, it may be confusing. Confused by certain situations that arise

The above is the detailed content of What is monkey patching in python? how to use?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete