Monkeypatching
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."
Related Terms
Anti-Pattern
Anti-Pattern is the opposite of a design pattern — it's a commonly used approach that looks like it solves a problem but actually makes things worse.
Hack
In coding, a hack is a clever but ugly solution that works without being pretty or proper. It is duct tape on a leaky pipe.
Mock
A mock is a fake version of something your code talks to.
Workaround
A workaround is a way to avoid a problem without actually fixing it. The door is broken so you use the window.