Monkey patch is the act of dynamically modifying the runtime code (not the source code) of a dynamic programming language, and it is the information (data/code) used to modify the runtime code. Monkey patching adds or replaces programming aspects like methods, classes, attributes, and functions in memory. Modifying the runtime code allows for modifying the behavior of third-party software without maintaining a modified version of the source code.
The term monkey patch seems to have come from an earlier term, guerrilla patch, which referred to changing code sneakily – and possibly incompatibly with other such patches – at runtime. The word guerrilla, nearly homophonous with gorilla, became monkey, possibly to make the patch sound less intimidating.
Despite the name's suggestion, a monkey patch is sometimes the official method of extending a program. For example, web browsers such as Firefox and Internet Explorer used to encourage this, although today browsers (including Firefox) support extension differently.
Monkey patch varies depending upon context. In Ruby, Python, and other languages, monkey patch refers only to dynamic modification of a class or module at runtime, motivated by the intent to patch existing third-party code as a workaround to a bug or feature which does not act as desired. Other forms of modifying classes at runtime have different names. For example, in Zope and Plone, security patches are often delivered using dynamic class modification, but they are called hot fixes.[citation needed]
Some pitfalls of monkey patching:
The following monkey patches the value of pi in the standard Python math library to make it compliant with the Indiana pi bill.
>>> import math
>>> math.pi
3.141592653589793
>>> math.pi = 3.2 # monkey-patch the value of Pi in the math module
>>> math.pi
3.2
The next time Python is started, the value of pi will be what it was before the patch: 3.141592653589793.
when someone created a guerrilla patch very carefully and tried to avoid any battles, they tried to make it sound less forceful by calling it a monkey patch