In an existing code-base, sometimes I would like to change fixed values into stochastic values. As an oversimplified example:
def existing_function(v):
return v + 1
# Normal usage:
v = 1
existing_function(v)
2
existing_function(v)
2
# 'Stochastic' usage
v = aStochasticVariable()
class X:
def __init__(self, v):
self. v = v
def existing_function(self):
return self.v
o= X(v)
o.existing_function()
5
o.existing_function()
9
I was thinking of sub classing for example the builtin (int or float) but I don't know how to get the value of (eg) the int (what is the name of the variable that holds the value of the int?). Another options would be to use the dunder call as a property:
class StochasticInt(int):
@property
def __call__(self):
return random.randint()
But this does not work. I also can't find a solution in the known libs, so ... anybody any ideas, thoughts?
Kind regards, vA