Questions tagged [monkeypatching]

Dynamically modifying run-time behavior by replacing program elements with new program elements

Monkeypatching, in late-bound languages, refers to modifying run-time behavior by replacing program elements with new program elements.

Examples include replacing a method on a class with a new function, or patching a member of a class.

The term may have a pejorative connotation, as the technique is usually used on third-party code.

For more discussion, see "Monkeypatching for Humans" on Coding Horror.

949 questions
842
votes
19 answers

Adding a method to an existing object instance in Python

How do I add a method to an existing object (i.e., not in the class definition) in Python? I understand that it's not generally considered good practice to do so, except in some cases.
akdom
  • 32,264
  • 27
  • 73
  • 79
826
votes
8 answers

What is monkey patching?

I am trying to understand, what is monkey patching or a monkey patch? Is that something like methods/operators overloading or delegating? Does it have anything common with these things?
Sergei Basharov
  • 51,276
  • 73
  • 200
  • 335
495
votes
3 answers

When monkey patching an instance method, can you call the overridden method from the new implementation?

Say I am monkey patching a method in a class, how could I call the overridden method from the overriding method? I.e. Something a bit like super E.g. class Foo def bar() "Hello" end end class Foo def bar() super() + " World" …
James Hollingworth
  • 14,040
  • 12
  • 39
  • 57
305
votes
25 answers

How to add property to a class dynamically?

The goal is to create a mock class which behaves like a db resultset. So for example, if a database query returns, using a dict expression, {'ab':100, 'cd':200}, then I would like to see: >>> dummy.ab 100 At first I thought maybe I could do it…
Anthony Kong
  • 37,791
  • 46
  • 172
  • 304
119
votes
12 answers

Can I patch a Python decorator before it wraps a function?

I have a function with a decorator that I'm trying test with the help of the Python Mock library. I'd like to use mock.patch to replace the real decorator with a mock 'bypass' decorator which just calls the function. What I can't figure out is how…
Chris Sears
  • 6,502
  • 5
  • 32
  • 35
99
votes
6 answers

Monkey patching a class in another module in Python

I'm working with a module written by someone else. I'd like to monkey patch the __init__ method of a class defined in the module. The examples I have found showing how to do this have all assumed I'd be calling the class myself (e.g. Monkey-patch…
Snorfalorpagus
  • 3,348
  • 2
  • 29
  • 51
98
votes
8 answers

Can I add custom methods/attributes to built-in Python types?

For example—say I want to add a helloWorld() method to Python's dict type. Can I do this? JavaScript has a prototype object that behaves this way. Maybe it's bad design and I should subclass the dict object, but then it only works on the subclasses…
jedmao
  • 10,224
  • 11
  • 59
  • 65
92
votes
5 answers

How does one monkey patch a function in python?

I'm having trouble replacing a function from a different module with another function and it's driving me crazy. Let's say I have a module bar.py that looks like this: from a_package.baz import do_something_expensive def a_function(): print…
guidoism
  • 7,820
  • 8
  • 41
  • 59
71
votes
8 answers

What does 'Monkey Patching' exactly Mean in Ruby?

According to Wikipedia, a monkey patch is: a way to extend or modify the runtime code of dynamic languages [...] without altering the original source code. The following statement from the same entry confused me: In Ruby, the term monkey…
Yaser Sulaiman
  • 3,311
  • 2
  • 28
  • 25
68
votes
4 answers

Can you patch *just* a nested function with closure, or must the whole outer function be repeated?

A 3rd party library we use contains a rather long function that uses a nested function inside it. Our use of that library triggers a bug in that function, and we very much would like to solve that bug. Unfortunately, the library maintainers are…
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
64
votes
6 answers

Making object JSON serializable with regular encoder

The regular way of JSON-serializing custom non-serializable objects is to subclass json.JSONEncoder and then pass a custom encoder to json.dumps(). It usually looks like this: class CustomEncoder(json.JSONEncoder): def default(self, obj): …
leonsas
  • 4,718
  • 6
  • 43
  • 70
62
votes
7 answers

Monkey patching a @property

Is it at all possible to monkey patch the value of a @property of an instance of a class that I do not control? class Foo: @property def bar(self): return here().be['dragons'] f = Foo() print(f.bar) # baz f.bar = 42 #…
deceze
  • 510,633
  • 85
  • 743
  • 889
59
votes
4 answers

Monkey-patch Python class

I've got a class, located in a separate module, which I can't change. from module import MyClass class ReplaceClass(object) ... MyClass = ReplaceClass This doesn't change MyClass anywhere else but this file. However if I'll add a method like…
ZooKeeper
  • 685
  • 1
  • 6
  • 7
57
votes
15 answers

Can you monkey patch methods on core types in Python?

Ruby can add methods to the Number class and other core types to get effects like this: 1.should_equal(1) But it seems like Python cannot do this. Is this true? And if so, why? Does it have something to do with the fact that type can't be…
airportyh
  • 21,948
  • 13
  • 58
  • 72
56
votes
1 answer

What is the difference between mocking and monkey patching?

I work with python and I'm a bit new to testing. I often see tests replacing an external dependency with a local method like so: import some_module def get_file_data(): return "here is the pretend file data" some_module.get_file_data =…
Jad S
  • 2,705
  • 6
  • 29
  • 49
1
2 3
63 64