什么是Monkey修补?

问题:什么是Monkey修补?

我想了解什么是Monkey补丁或Monkey补丁?

是方法/运算符重载还是委派?

这些东西有什么共同点吗?

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 anything common with these things?


回答 0

不,这与这些东西都不一样。它只是在运行时动态替换属性。

例如,考虑一个具有method的类get_data。该方法进行外部查找(例如,在数据库或Web API上),并且类中的各种其他方法都调用它。但是,在单元测试中,您不希望依赖于外部数据源-因此您可以用get_data返回一些固定数据的存根动态替换该方法。

因为Python类是可变的,并且方法只是类的属性,所以您可以随意执行此操作-实际上,您甚至可以以完全相同的方式替换模块中的类和函数。

但是,正如评论员指出的那样,在进行Monkey修补时要格外小心:

  1. 如果除了测试逻辑调用之外还有其他要求get_data,它还会调用Monkey修补的替代品,而不是原始替代品-可能是好是坏。提防。

  2. 如果存在某个变量或属性get_data,在您替换它时也指向该函数,则该别名将不会更改其含义,并且将继续指向原始get_data。(为什么?Python只是get_data将类中的名称重新绑定到其他函数对象;其他名称绑定完全不受影响。)

No, it’s not like any of those things. It’s simply the dynamic replacement of attributes at runtime.

For instance, 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.

Because Python classes are mutable, and methods are just attributes of the class, you can do this as much as you like – and, in fact, you can even replace classes and functions in a module in exactly the same way.

But, as a commenter pointed out, use caution when monkeypatching:

  1. If anything else besides your test logic calls get_data as well, it will also call your monkey-patched replacement rather than the original — which can be good or bad. Just beware.

  2. If some variable or attribute exists that also points to the get_data function by the time you replace it, this alias will not change its meaning and will continue to point to the original get_data. (Why? Python just rebinds the name get_data in your class to some other function object; other name bindings are not impacted at all.)


回答 1

MonkeyPatch是一段Python代码,可在运行时(通常在启动时)扩展或修改其他代码。

一个简单的示例如下所示:

from SomeOtherProduct.SomeModule import SomeClass

def speak(self):
    return "ook ook eee eee eee!"

SomeClass.speak = speak

资料来源: Zope Wiki上的MonkeyPatch页面。

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.


回答 2

什么是Monkey补丁?

简而言之,Monkey修补程序是在程序运行时对模块或类进行更改。

使用示例

在Pandas文档中有一个Monkey修补的示例:

import pandas as pd
def just_foo_cols(self):
    """Get a list of column names containing the string 'foo'

    """
    return [x for x in self.columns if 'foo' in x]

pd.DataFrame.just_foo_cols = just_foo_cols # monkey-patch the DataFrame class
df = pd.DataFrame([list(range(4))], columns=["A","foo","foozball","bar"])
df.just_foo_cols()
del pd.DataFrame.just_foo_cols # you can also remove the new method

为了解决这个问题,首先我们导入模块:

import pandas as pd

接下来,我们创建一个方法定义,该定义在任何类定义的范围之外都是未绑定的和自由的(由于函数和未绑定方法之间的区别是毫无意义的,Python 3取消了未绑定方法):

def just_foo_cols(self):
    """Get a list of column names containing the string 'foo'

    """
    return [x for x in self.columns if 'foo' in x]

接下来,我们简单地将该方法附加到要在其上使用的类:

pd.DataFrame.just_foo_cols = just_foo_cols # monkey-patch the DataFrame class

然后,我们可以在类的实例上使用方法,并在完成后删除该方法:

df = pd.DataFrame([list(range(4))], columns=["A","foo","foozball","bar"])
df.just_foo_cols()
del pd.DataFrame.just_foo_cols # you can also remove the new method

注意改名

如果您正在使用名称处理(带有双下划线的前缀属性,这会更改名称,并且我不建议这样做),则必须手动命名。由于我不建议使用名称修饰,因此在此不再进行演示。

测试例

我们如何在测试中使用这些知识?

假设我们需要模拟对外部数据源的数据检索调用,该调用会导致错误,因为我们要确保在这种情况下的正确行为。我们可以Monkey修补数据结构以确保这种行为。(因此,使用与Daniel Roseman建议的类似的方法名称:)

import datasource

def get_data(self):
    '''monkey patch datasource.Structure with this to simulate error'''
    raise datasource.DataRetrievalError

datasource.Structure.get_data = get_data

当我们测试它的行为是否依赖于此方法引发错误时,如果正确实施,我们将在测试结果中获得该行为。

只需执行上述操作,就Structure可以在整个过程中更改对象,因此,您需要在单元测试中使用设置和拆卸来避免这样做,例如:

def setUp(self):
    # retain a pointer to the actual real method:
    self.real_get_data = datasource.Structure.get_data
    # monkey patch it:
    datasource.Structure.get_data = get_data

def tearDown(self):
    # give the real method back to the Structure object:
    datasource.Structure.get_data = self.real_get_data

(虽然上面是好的,它很可能是一个更好的主意,使用mock图书馆修补代码mockpatch装饰会少些误差比上面做容易,这需要更多的代码,从而有更多机会引入错误我尚未审查其中的代码,mock但我想它以类似的方式使用了Monkey修补程序。)

What is a monkey patch?

Simply put, monkey patching is making changes to a module or class while the program is running.

Example in usage

There’s an example of monkey-patching in the Pandas documentation:

import pandas as pd
def just_foo_cols(self):
    """Get a list of column names containing the string 'foo'

    """
    return [x for x in self.columns if 'foo' in x]

pd.DataFrame.just_foo_cols = just_foo_cols # monkey-patch the DataFrame class
df = pd.DataFrame([list(range(4))], columns=["A","foo","foozball","bar"])
df.just_foo_cols()
del pd.DataFrame.just_foo_cols # you can also remove the new method

To break this down, first we import our module:

import pandas as pd

Next we create a method definition, which exists unbound and free outside the scope of any class definitions (since the distinction is fairly meaningless between a function and an unbound method, Python 3 does away with the unbound method):

def just_foo_cols(self):
    """Get a list of column names containing the string 'foo'

    """
    return [x for x in self.columns if 'foo' in x]

Next we simply attach that method to the class we want to use it on:

pd.DataFrame.just_foo_cols = just_foo_cols # monkey-patch the DataFrame class

And then we can use the method on an instance of the class, and delete the method when we’re done:

df = pd.DataFrame([list(range(4))], columns=["A","foo","foozball","bar"])
df.just_foo_cols()
del pd.DataFrame.just_foo_cols # you can also remove the new method

Caveat for name-mangling

If you’re using name-mangling (prefixing attributes with a double-underscore, which alters the name, and which I don’t recommend) you’ll have to name-mangle manually if you do this. Since I don’t recommend name-mangling, I will not demonstrate it here.

Testing Example

How can we use this knowledge, for example, in testing?

Say we need to simulate a data retrieval call to an outside data source that results in an error, because we want to ensure correct behavior in such a case. We can monkey patch the data structure to ensure this behavior. (So using a similar method name as suggested by Daniel Roseman:)

import datasource

def get_data(self):
    '''monkey patch datasource.Structure with this to simulate error'''
    raise datasource.DataRetrievalError

datasource.Structure.get_data = get_data

And when we test it for behavior that relies on this method raising an error, if correctly implemented, we’ll get that behavior in the test results.

Just doing the above will alter the Structure object for the life of the process, so you’ll want to use setups and teardowns in your unittests to avoid doing that, e.g.:

def setUp(self):
    # retain a pointer to the actual real method:
    self.real_get_data = datasource.Structure.get_data
    # monkey patch it:
    datasource.Structure.get_data = get_data

def tearDown(self):
    # give the real method back to the Structure object:
    datasource.Structure.get_data = self.real_get_data

(While the above is fine, it would probably be a better idea to use the mock library to patch the code. mock‘s patch decorator would be less error prone than doing the above, which would require more lines of code and thus more opportunities to introduce errors. I have yet to review the code in mock but I imagine it uses monkey-patching in a similar way.)


回答 3

根据维基百科

在Python中,“Monkey补丁”一词仅指在运行时对类或模块的动态修改,其目的是为了修补现有的第三方代码,以作为无法按需使用的错误或功能的解决方法。

According to Wikipedia:

In Python, 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 you desire.


回答 4

首先:Monkey修补是一个邪恶的hack(我认为)。

它通常用于用自定义实现替换模块或类级别的方法。

最常见的用例是在无法替换原始代码时为模块或类中的错误添加解决方法。在这种情况下,您可以通过Monkey补丁替换“错误的”代码,并将其替换为您自己的模块/包中的实现。

First: monkey patching is an evil hack (in my opinion).

It is often used to replace a method on the module or class level with a custom implementation.

The most common usecase is adding a workaround for a bug in a module or class when you can’t replace the original code. In this case you replace the “wrong” code through monkey patching with an implementation inside your own module/package.


回答 5

Monkey补丁只能用动态语言完成,其中python是一个很好的例子。在运行时更改方法而不是更新对象定义是一个示例;类似地,在运行时添加属性(无论方法还是变量)也被视为Monkey补丁。这些操作通常在使用您没有来源的模块时完成,这样就无法轻易更改对象定义。

这被认为是不好的,因为这意味着对象的定义不能完全或准确地描述其实际行为。

Monkey patching can only be done in dynamic languages, of which python is a good example. Changing a method at runtime instead of updating the object definition is one example;similarly, adding attributes (whether methods or variables) at runtime is considered monkey patching. These are often done when working with modules you don’t have the source for, such that the object definitions can’t be easily changed.

This is considered bad because it means that an object’s definition does not completely or accurately describe how it actually behaves.


回答 6

Monkey修补程序是在运行时重新打开类中的现有类或方法并更改其行为,应谨慎使用它们,或者仅在确实需要时使用它。

由于Python是一种动态编程语言,因此类是可变的,因此您可以重新打开它们并进行修改甚至替换。

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.


回答 7

什么是Monkey修补?Monkey修补程序是一种用于在运行时动态更新一段代码的行为的技术。

为什么要使用Monkey补丁?它允许我们在运行时修改或扩展库,模块,类或方法的行为,而无需实际修改源代码

结论Monkey修补是一种很酷的技术,现在我们已经学习了如何在Python中进行修补。但是,正如我们所讨论的,它有其自身的缺点,应谨慎使用。

有关更多信息,请参考[1]:https : //medium.com/@nagillavenkatesh1234/monkey-patching-in-python-explained-with-examples-25eed0aea505

What is monkey patching? Monkey patching is a technique used to dynamically update the behavior of a piece of code at run-time.

Why use monkey patching? It allows us to modify or extend the behavior of libraries, modules, classes or methods at runtime without actually modifying the source code

Conclusion Monkey patching is a cool technique and now we have learned how to do that in Python. However, as we discussed, it has its own drawbacks and should be used carefully.

For more info Please refer [1]: https://medium.com/@nagillavenkatesh1234/monkey-patching-in-python-explained-with-examples-25eed0aea505