Explaining monkey patching in Python with simple examples so that everyone can understand. In this tutorial, I will mention its use-case where you can use it in your project or program.

  1. Python Method May Be Static
  2. Python Monkey Patch Static Method Tutorial
  3. Python Monkey Patch Static Method Pdf
  4. Iron On Patches

What is Monkey Patching?

Monkey patching is a dynamic technique in Python by which you can modify the behavior of an existing class or module.

Since Python 3.8, AsyncMock. Unfortunately datetime.date is written in C, and so I couldn’t just monkey-patch out the static date.today method. This comprehensive guide to Python mocking will help you keep your unit tests straightforward and explain how to best use the mock.unittest library to your advantage.

To change the behavior of class or module, you don’t need to change the code inside class or method.

Let’s see how you can do that.

Method

I’m taking two coding examples to change the behavior of the class and module.

Example: Monkey Patching Python Class

Python Program To Change the behavior of Class Dynamically:

Output:

In the above program, we have created an instance objMonkey of class monkey.

We are assigning a new method name (patchFunc) to the existing class method name (monkeyFunc). The linking of a new method to the existing class method name happens at runtime. So the behavior of the class monkey is changed dynamically.

In the above class, you are changing the behavior of the class method defined in the same program file.

Static

Now let’s take another example of a monkey patching Python module.

Example: Monkey Ptaging Python Module

Write your own modules called animals.

Save this file as animals.py
Now import this file as Python module in your another Python program.

Output:

We are getting the same output. This time we have changed the behavior of the method defined inside the module animals without changing a single line of code inside the animals.py.

In this example, I tried the monkey patching my own module. Similarly, you can try it on existing standard modules.

[Use-Case] What’s the use of monkey patching?

Let’s take an example.

Suppose you are importing the standard module. Modules have different methods.

Method

As per your requirement, you want the method inside the module to work differently. It’s not easy to go to the method defined inside the standard module and change it.

The simplest way of doing the same things is by monkey patching technique.

How does it work?

Python Method May Be Static

Python monkey patch static method template

Python Monkey Patch Static Method Tutorial

  • You write your own method.
  • Assign this method name to the method name defined inside the module.
  • Now every time when you call the method defined inside the module, it calls your own method.

You changed the behavior of the method defined in the module.

Python Monkey Patch Static Method Pdf

Conclusion

This is all about monkey patching in Python- a technique to change the behavior of class or module.

Iron On Patches

There is also another technique called decorators in Python to change the behavior of code without changing the actual code dynamically.