I’ve encountered a challenge in doing repeated simulations with a changing parameter value using Python. I’m looking for a clean way to change the parameter value.
Reducing it to the simplest case, we can assume the parameter is showing up as the value for some specific keys in a dictionary, D
, let’s say D[1]
and D[2]
are both delta
. I need to rerun the code with a changed value of delta
, but updating D
in practice is not as simple in my real context as updating D[1]
and D[2]
:
Here’s the question:
Is there a way to create a dict D
with some values equal to a parameter delta
so that if I change delta
later, the corresponding values in D
are automatically updated? Basically I’m sort of trying to set the value of D
to be passed by reference.
More context:
Right now I have something like:
delta = 1
D = {1:delta, 2:delta, 3:1} #the real process of creating `D` is computationally slow.
#... some arbitrary code here, some of which I can't modify that needs D[1] to behave like a number.
delta = 2
D = {1:delta, 2:delta, 3:1}
#... equivalent code here
I can’t really wrap this in a for loop because I’m actually doing this in a Jupyter notebook and I want to include a markdown cell describing the outputs each time I change delta. Also creating D
is pretty involved.
My best solution right now is to track which keys correspond to delta in a separate list and then go through and update all of them. This process will be a touch messy and might cause confusion for some users who will not be all that familiar with python.
I don’t think it is possible to do what I’m asking, but if so it would make my life much easier.