# Best way to update parameter values in a temporary module

**URL:** <https://discourse.julialang.org/t/best-way-to-update-parameter-values-in-a-temporary-module/83971>\
**Category:** General Usage\
**Tags:** workflow, scripting\
**Created:** [July 8, 2022, 8:12pm UTC](https://discourse.julialang.org/t/best-way-to-update-parameter-values-in-a-temporary-module/83971 "2022-07-08T20:12:48Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jinml](https://avatars.discourse-cdn.com/v4/letter/j/91b2a8/32.png) [@jinml](https://discourse.julialang.org/u/jinml)\
**Post date:** [July 8, 2022, 8:12pm UTC](https://discourse.julialang.org/t/best-way-to-update-parameter-values-in-a-temporary-module/83971/1 "2022-07-08T20:12:48Z")

</div>

I am a Julia user mostly interested in numerical prototyping, where I have constant need to work with a large collection of custom functions that are related to each other, but far from being ready to define a package. I currently wrap the whole collection of codes in a temporary module in a `.jl` file in my search path to allow `Revise` detect any changes. That worked fine but pretty awkward.

Often times I need to quickly change certain values within the module while working in a REPL to test these changes on-spot. My question is how to do this more efficiently from REPL without constantly changing the source code. I could write a function like this to automatically go through a large number of `param` values and check the results in a loop.

```julia
module tempModule
...
... some_code_using_`param` ...
...
function change_param(param_new)
global param=param_new
... update_module ...
end
...
end #module

```

According to this [thread](https://gist.github.com/flcong/2eba0189d7d3686ea9633a6d14398931), global variables wrapped in a function (or module, supposedly) should be efficient. However, I know that using global variables are generally discouraged.  
Is there a better way of doing this without changing the source code?

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [July 8, 2022, 8:55pm UTC](https://discourse.julialang.org/t/best-way-to-update-parameter-values-in-a-temporary-module/83971/2 "2022-07-08T20:55:40Z")

</div>

You can `eval` _into_ an existing module to redefine functions, but if you’re just trying to change a single value then using a `Ref` is pretty convenient:

```julia
julia> module MyModule
         const x = Ref(1.0)
         
         g(y) = y + x[]
       end
Main.MyModule

julia> MyModule.g(1)
2.0

julia> MyModule.x[] = 2
2

julia> MyModule.g(1)
3.0

```

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [July 8, 2022, 8:56pm UTC](https://discourse.julialang.org/t/best-way-to-update-parameter-values-in-a-temporary-module/83971/3 "2022-07-08T20:56:36Z")

</div>

And here’s a version using `eval` to redefine a single function within the module:

```julia
julia> module MyModule
         x() = 1.0
         
         g(y) = y + x()
       end
Main.MyModule

julia> MyModule.g(1)
2.0

julia> @eval MyModule x() = 2.0
x (generic function with 1 method)

julia> MyModule.g(1)
3.0

```

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [July 8, 2022, 9:01pm UTC](https://discourse.julialang.org/t/best-way-to-update-parameter-values-in-a-temporary-module/83971/4 "2022-07-08T21:01:21Z")

</div>

> [@jinml](#):
>
> According to this [thread](https://gist.github.com/flcong/2eba0189d7d3686ea9633a6d14398931), global variables wrapped in a function (or module, supposedly) should be efficient

Mm, that thread isn’t particularly informative in this case. Your code as posted will absolutely have performance problems due to `param` being a non-const global variable. The fact that you _access_ that global within a function is not helpful.

The `Ref` trick avoids the problem–you can safely do `const x = Ref(1.0)` and then mutate `x` later with `x[] = 2.0`. This is completely fine because `const` doesn’t stop you from mutating the _value_ of `x`; it only promises that you will not assign the label `x` to some new value entirely.

---

<div class="post-metadata">

**Author:** ![jinml](https://avatars.discourse-cdn.com/v4/letter/j/91b2a8/32.png) [@jinml](https://discourse.julialang.org/u/jinml)\
**Post date:** [July 8, 2022, 9:37pm UTC](https://discourse.julialang.org/t/best-way-to-update-parameter-values-in-a-temporary-module/83971/5 "2022-07-08T21:37:08Z")

</div>

> [@rdeits](#):
>
> Your code as posted will absolutely have performance problems due to `param` being a non-const global variable.

However, according to the second benchmark case in the thread, _Wrap the code in a function_, the function `f` referring to the parameters outside its own scope seems to perform as well as if those parameters were `const` global variables? I imagine it to be similar if the parameters were wrapped within a module?
