What is monkey patching in Python

爱喝马黛茶的安东尼
Release: 2020-01-04 17:45:50
forward
3084 people have browsed it

What is monkey patching in Python

The dynamic replacement of attributes at runtime is called a monkey patch.

Why is it called monkey patch

#The runtime replacement of attributes has nothing to do with monkeys. There are two opinions on the origin of monkey patch found online. :

1. This word was originally Guerrilla Patch, a miscellaneous army and guerrilla army, which means that this part is not original. In English, the pronunciation of guerilla is similar to gollia (orangutan), and later it was written as monkey (monkey). .

2. Another explanation is that because this method messes up the original code (messing with it), it is called monkeying about (naughty) in English, so it is called Monkey Patch.

The name of monkey patch is a bit confusing, as long as it corresponds to the "function of module runtime replacement".

Usage of monkey patch

1. Method of dynamically replacing modules at runtime

stackoverflow There are two popular examples above.

consider a class that has a method get_data. This method does an
external lookup (on a database or web API, for example), and various
other methods in the class call it. However, in a unit test, you don't
want to depend on the external data source - so you dynamically
replace the get_data method with a stub that returns some fixed data.
Copy after login

Suppose a class has a method get_data. This method does some external queries (such as querying the database or Web API, etc.), and many other methods in the class call it. However, in a unit test, you don't want to rely on external data sources. So you replace this get_data method with a dumb method state, which just returns some test data.

Another example quoted is the explanation of Monkey Patch on Zope wiki:

from SomeOtherProduct.SomeModule import SomeClass
def speak(self):
    return "ook ook eee eee eee!"
SomeClass.speak = speak
Copy after login

There is also a more practical example. Many codes use import json. Later, it was found that ujson has higher performance. If I feel that it is more expensive to change the import json of each file to import ujson as json, or if I want to test whether replacing json with ujson meets expectations, I only need to add:

import json
import ujson
def monkey_patch_json():
    json.__name__ = 'ujson'
    json.dumps = ujson.dumps
    json.loads = ujson.loads
monkey_patch_json()
Copy after login

2 at the entrance. There are many ways to dynamically add modules at runtime

There are many scenarios like this. For example, if we reference a module in the team's general library and want to enrich the module's functions, in addition to inheritance, we can also consider using Monkey. Patch.

Personally, I feel that while Monkey Patch brings convenience, it also risks messing up the elegance of the source code.

PHP Chinese website has a large number of free Python video tutorials, everyone is welcome to learn!

This article is reproduced from: https://www.jianshu.com/p/a19f936471e4

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

Related labels:
source:jianshu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!