Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Best way to deal with functions with long list of arguments?

I've found various detailed explanations on how to pass long lists of arguments into a function, but I still kinda doubt if that's proper way to do it.

In other words, I suspect that I'm doing it wrong, but I can't see how to do it right.

The problem: I have (not very long) recurrent function, which uses quite a number of variables and needs to modify some content in at least some of them.

What I end up with is sth like this:

def myFunction(alpha, beta, gamma, zeta, alphaList, betaList, gammaList, zetaList):
    <some operations>
    myFunction(alpha, beta, modGamma, zeta, modAlphaList, betaList, gammaList, modZetaList)

...and I want to see the changes I did on original variables (in C I would just pass a reference, but I hear that in Python it's always a copy?).

Sorry if noob, I don't know how to phrase this question so I can find relevant answers.

like image 690
StanTastic Avatar asked Jun 21 '13 11:06

StanTastic


People also ask

How do you pass a list of arguments to a function in Python?

In Python, you can unpack list , tuple , dict (dictionary) and pass its elements to function as arguments by adding * to list or tuple and ** to dictionary when calling function.

How many function arguments is too many Python?

Functions with three arguments (triadic function) should be avoided if possible. More than three arguments (polyadic function) are only for very specific cases and then shouldn't be used anyway.

Can function take list arguments Python?

You can send any data types of argument to a function (string, number, list, dictionary etc.), and it will be treated as the same data type inside the function.


3 Answers

You could wrap up all your parameters in a class, like this:

class FooParameters:
    alpha = 1.0
    beta = 1.0
    gamma = 1.0
    zeta = 1.0
    alphaList = []
    betaList = []
    gammaList = []
    zetaList = []

and then your function takes a single parameter instance:

def myFunction(params):
    omega = params.alpha * params.beta + exp(params.gamma)
    # more magic...

calling like:

testParams = FooParameters()
testParams.gamma = 2.3
myFunction(testParams)
print params.zetaList

Because the params instance is passed by reference, changes in the function are preserved.

like image 104
gavinb Avatar answered Oct 13 '22 01:10

gavinb


This is commonly used in matplotlib, for example. They pass the long list of arguments using * or **, like:

def function(*args, **kwargs):
    do something

Calling function:

function(1,2,3,4,5, a=1, b=2, b=3)

Here 1,2,3,4,5 will go to args and a=1, b=2, c=3 will go to kwargs, as a dictionary. So that they arrive at your function like:

args = [1,2,3,4,5]
kwargs = {a:1, b:2, c:3}

And you can treat them in the way you want.

like image 35
Saullo G. P. Castro Avatar answered Oct 12 '22 23:10

Saullo G. P. Castro


I don't know where you got the idea that Python copies values when passing into a function. That is not at all true.

On the contrary: each parameter in a function is an additional name referring to the original object. If you change the value of that object in some way - for example, if it's a list and you change one of its members - then the original will also see that change. But if you rebind the name to something else - say by doing alpha = my_completely_new_value - then the original remains unchanged.

like image 35
Daniel Roseman Avatar answered Oct 13 '22 00:10

Daniel Roseman