Python Topics : Monkey Patching
Introduction to Monkey Patching

monkey patching is a technique that allows modification or extention of the behavior of existing modules, classes, or functions at runtime without changing the original source code

Applying Monkey Patching
steps needed to apply the monkey patching
  1. identify the target to be patched
  2. create the patch by writing code to add, modify, or replace existing logic
  3. apply the patch by using an assignment to apply it to the target
    the patch will overwrite or extend the existing behavior
monkey patching is a powerful tool
should use it carefully to avoid unexpected behaviors

Monkey Patching Example
monkey patch the Robot type with the add_speech function
def add_speech(cls):
    cls.speak = lambda self, message: print(message)
    return cls

class Robot:
    def __init__(self, name):
        self.name = name

# assign the patch to the type
Robot = add_speech(Robot)

robot = Robot('Optimus Prime')
robot.speak('Hi')
can also use a decorator to make the assign the monkey patch
def add_speech(cls):
    cls.speak = lambda self, message: print(message)
    return cls

@add_speech
class Robot:
    def __init__(self, name):
        self.name = name

robot = Robot('Optimus Prime')
robot.speak('Hi')
When to use Monkey Patching
should use monkey patching only when necessary because it can make code harder to understand and debug

if using a third-party library and it has an urgent bug and cannot wait for official release
can use the monkey patching to apply quick fixes while waiting for a proper solution

want to add functionality to classes and cannot use other techniques like inheritance or composition

index