Python Gone Bananas: Monkey Patching Isn't Monkey Business

02:15 PM - 03:10 PM on July 17, 2016, Room CR5

James Powell

Audience level:


Python is an extremely dynamic language with a very "rich runtime." Unlike in a compiled language like C or go, symbols (functions and data objects) can be bound and rebound at runtime; swapped out without the need for complicated syntax and without the risk of unsafe complications. In the Python community, this is called "monkey-patching." But it's not as bananas as it sounds! In fact, it can be a very useful mechanism for extending functionality or for making robust, surgical fixes to a code-base!


Python is a "system" language (as opposed to a "systems" language)—a language that is well-suited for building large, complex systems. As with any major construction project, a mature "system" will have "hooks" or "safety valves" or "escape hatches." These will be provided so that engineers can safely and surgically implement custom behavior, and also so that they may account for model errors that make it into production. Python differs from historical languages like C in that it provides a very rich runtime. This runtime offers programmers many mechanisms for extending or modifying the behavior of the system, and these mechanisms are safe, reliable, robust, and offered through the standard library and through standard language constructs. This talk discusses one such mechanism: monkey-patching. What is monkey-patching? Where is it useful? What are its limitations? What does it tell us about Python?