Skip to content

Monkeypatching

Medium — good to knowGeneral Dev

ELI5 — The Vibe Check

Changing how existing code works at runtime by overwriting functions or objects — without touching the original source code. Like sneaking into a restaurant's kitchen and changing a recipe while the food is being served. Sometimes useful for quick fixes, always dangerous, frequently regretted.

Real Talk

Monkeypatching is the runtime modification of classes, modules, or functions in dynamic languages. Common in Python and Ruby, it can override methods, add attributes, or change behavior of third-party code. While useful for testing and hotfixes, it creates maintenance and debugging challenges.

Show Me The Code

# Python monkeypatch
import datetime

# Override datetime.now() for testing
original = datetime.datetime.now
datetime.datetime.now = lambda: datetime.datetime(2025, 1, 1)

print(datetime.datetime.now())  # 2025-01-01 00:00:00

# Restore it!
datetime.datetime.now = original

When You'll Hear This

"We monkeypatched the logger to suppress output in tests." / "Monkeypatching in production? That's brave."

Made with passive-aggressive love by manoga.digital. Powered by Claude.