David W
David W

Reputation: 11

functions with multiple parameters

It seems like there should be a simple answer to this but I'm not finding it. I have a set of equations with two or three variables of interest (say, x, y, and z) and a whole bunch of parameters (a,b,c, . . . z) that are used for calibrating the model that the equations represent. I am able write down functions with the form "def func(variables, parameters): return stuff", and do what I want with them, which is solving for say, x given y and z for a given choice of parameters. I also need to be able to change the parameters as the model calibration changes and also test how the model output changes over a range of parameters. One other piece of info, which is that I need to use something like fsolve to find where the equations intersect to solve the model.

This works fine but is impossible to read, because each function has a super-long list of inputs. I want the code to be understandable to others. Is there a way to have the function read a sequence of parameters so that the functions take the form func(x,y,z, params)?

Upvotes: 0

Views: 207

Answers (1)

holdenweb
holdenweb

Reputation: 37003

one easy way to simplify your calls would be to use simple objects and set attributes on it.

class Params:
    pass

p = Params()
p.a = 'some value'
    ...
p.q = 'some other value'

You can then define your function using

def myfunc(x, y, z, params):
    ....
    something = x+y+z+params.q

You would call the function like

result = myfunc(1.2, 3.4, 5.6, p)

This will allow you to access the individual parameters as attributes of the params parameter, making the calls clearer.

Upvotes: 1

Related Questions