# Is monkey patching possible in julia 0.5?

**URL:** <https://discourse.julialang.org/t/is-monkey-patching-possible-in-julia-0-5/600>\
**Category:** General Usage\
**Tags:** testing\
**Created:** [November 27, 2016, 11:00pm UTC](https://discourse.julialang.org/t/is-monkey-patching-possible-in-julia-0-5/600 "2016-11-27T23:00:58Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![pdobacz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdobacz/32/1775_2.png) [@pdobacz](https://discourse.julialang.org/u/pdobacz)\
**Post date:** [November 27, 2016, 11:00pm UTC](https://discourse.julialang.org/t/is-monkey-patching-possible-in-julia-0-5/600/1 "2016-11-27T23:00:58Z")

</div>

Is there any means to (temporarily) redefine a function, so that:

```julia
f = function(x) 0.5 end
# monkey patch Base.sin with f
sin(12.0) # returns 0.5
# undo monkey patching

```

I suppose it was once possible, done like here: [Fixtures.jl/patch.jl at master · burrowsa/Fixtures.jl · GitHub](https://github.com/burrowsa/Fixtures.jl/blob/master/src/mock/patch.jl#L23)

Now, doing something similar is not possible anymore, since `sin` has no fields like `code, fptr, env`, which I assume defined the function back in 0.3.

I need such thing to temporarily override any function’s behavior to isolate tests from effects of stuff like `Base.open` etc.

One vague idea I have would be to redefine a function for particular argument types i.e. `import Base.sin`, redefine `sin(::Float64)`, test, undo (somehow). But that seems to require much more effort - to get the types right for the patch to work as intended. And it would produce redefinition warnings. Is there any other way?

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [November 27, 2016, 11:08pm UTC](https://discourse.julialang.org/t/is-monkey-patching-possible-in-julia-0-5/600/2 "2016-11-27T23:08:38Z")

</div>

No and you shouldn’t do it.

---

<div class="post-metadata">

**Author:** ![akis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/akis/32/156_2.png) [@akis](https://discourse.julialang.org/u/akis)\
**Post date:** [November 27, 2016, 11:24pm UTC](https://discourse.julialang.org/t/is-monkey-patching-possible-in-julia-0-5/600/3 "2016-11-27T23:24:06Z")

</div>

Probably not what you want, but in case your code happens to not depend on built-in calls to `Base.sin`, and if you recompile your own calls as needed, you could use `sin` as a variable:

```julia
f = function(x) 0.5 end
sin = f
sin(12.0) # returns 0.5
sin = Base.sin

```

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [November 28, 2016, 5:46pm UTC](https://discourse.julialang.org/t/is-monkey-patching-possible-in-julia-0-5/600/4 "2016-11-28T17:46:10Z")

</div>

You can also define `sin` as a different function in a module or in a local scope.

---

<div class="post-metadata">

**Author:** ![pdobacz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdobacz/32/1775_2.png) [@pdobacz](https://discourse.julialang.org/u/pdobacz)\
**Post date:** [November 29, 2016, 11:21pm UTC](https://discourse.julialang.org/t/is-monkey-patching-possible-in-julia-0-5/600/5 "2016-11-29T23:21:48Z")

</div>

@akis, @StefanKarpinski - yes, but I think that doesn’t affect calls to `sin` done in other modules. In my case that would be modules under test, where I would like to monkey patch calls to functions that break my test isolation. `sin` is a silly example, sorry. More likely candidate is `open` or calls to an HTTP requests package. Like so:

```julia
# inside my tests:

fake_get = function(someargs) #=pretend to make an HTTP request=# end
Requests.get = fake_get
# test my code that uses Requests.get, but without any HTTP requests actually made
# ...
Requests.get = ? # get the old Requests.get back

```

I am aware that this is by itself ugly, but how can you do a test as above and avoid monkey patching?

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [November 29, 2016, 11:47pm UTC](https://discourse.julialang.org/t/is-monkey-patching-possible-in-julia-0-5/600/6 "2016-11-29T23:47:56Z")

</div>

No, that kind of monkey patching you cannot do. Testing is usually handled with some combination of [mock objects](https://en.wikipedia.org/wiki/Mock_object) and [dependency injection](https://en.wikipedia.org/wiki/Dependency_injection).

---

<div class="post-metadata">

**Author:** ![akis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/akis/32/156_2.png) [@akis](https://discourse.julialang.org/u/akis)\
**Post date:** [November 29, 2016, 11:49pm UTC](https://discourse.julialang.org/t/is-monkey-patching-possible-in-julia-0-5/600/7 "2016-11-29T23:49:48Z")

</div>

“Traditional” monkey patching as found in other dynamic languages isn’t directly supported by Julia, for performance reasons. Maybe you have to “plan ahead” and design your code to provide that kind of testing as an actual **feature** of your application (e.g. to maintain some **state** of whether your application is running in **test mode** and “consciously” call functions according to current mode, probably calling the fake ones with the `invoke()` function), instead of considering it as an external temporary change. That way, the resulting performance regression from not having the fake functions available at compile time will affect mostly the test mode.

---

<div class="post-metadata">

**Author:** ![pdobacz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdobacz/32/1775_2.png) [@pdobacz](https://discourse.julialang.org/u/pdobacz)\
**Post date:** [November 30, 2016, 6:21am UTC](https://discourse.julialang.org/t/is-monkey-patching-possible-in-julia-0-5/600/8 "2016-11-30T06:21:59Z")

</div>

OK, thank you! So in this particular respect Julia is more “like Java” and “unlike Python”, all clear now.
