Python - enter the correct number of variables based on function descriptor

I have a list of variables and a function object, and I would like to assign the correct number of variables depending on the function.

def sum2(x,y): return x + y def sum3(x,y,z): return x + y + z varList = [1,2,3] 

so if f = sum2, I would like it to call the first 2 elements of varList, and if f = sum3, I would call it with 3 elements of the function.

+5
source share
6 answers

Use the inspect module as follows:

 import inspect n2 = len(inspect.getargspec(sum2)[0]) n3 = len(inspect.getargspec(sum3)[0]) sum2(*varList[0:n2]) sum3(*varList[0:n3]) 

getargspec returns a 4-tuple (args, varargs, keywords, defaults) . So the above code works if all your arguments are explicit, i.e. not * or ** args. If you have some of them, change the code accordingly.

+1
source

This can be done in one function if you always return the sum of all the arguments passed.

 def sum1(*args): return sum(args) 

This is just using positional arguments, since you obviously do not need to explicitly set individual values. It is also more flexible than the ZdaR solution, since you do not need to know in advance the maximum number of arguments that you can get.

Some examples:

 >>> print sum1(1, 2, 3) 6 >>> print sum1(1) 1 >>> print sum1(-1, 0, 6, 10) 15 
+3
source

You can use the default initialization, you must keep in mind the maximum number of variables that can be passed to this function. Then create a function with so many parameters, but initializing them with 0, because a+0 = a (in the absence of some parameters, it is then replaced with 0 , which will not affect the results.)

 def sum1(a=0, b=0, c=0, d=0): return a+b+c+d print sum1(1) >>> 1 print sum1(1, 2) >>> 3 print sum1(1, 2, 3) >>> 6 print sum1(1, 2, 3, 4) >>> 10 

However, if you call a function with more than 4 arguments, this will result in an error statement

Also, as @CoryKramer suggested in the comments, you can also pass your varlist = [1, 2, 3, 4] as a parameter:

 print sum1(*varlist) >>> 10 

Remembering that the value of len(varlist) must be less than the number of defined parameters.

+1
source

Common decision:

To get the number of arguments, you can use f.func_code.co_argcount, and then pass the correct items from the list:

 def sum2(x,y): return x + y def sum3(x,y,z): return x + y + z varlist = [2,5,4] [f(*varlist[:f.func_code.co_argcount]) for f in [sum2,sum3]] >> [7, 11] 
+1
source

You can check if f is a function with the keyword is

 def sum2(x, y): return x + y def sum3(x, y, z): return x + y + z varList = [1, 2, 3] f = sum2 if f is sum2: sum = f(varList[0], varList[1]) print('Sum 2: ' + str(sum)) # Prints: 'Sum 2: 3' f = sum3 if f is sum3: sum = f(varList[0], varList[1], varList[2]) print('Sum 3: ' + str(sum)) # Prints: 'Sum 3: 6' 
0
source

Function Attribute:

 def two(l): return l[0] + l[1] def three(l): return l[0] * l[1] + l[2] funcs = {2:two, 3:three} l = [1, 2, 3] print len(l) print funcs[len(l)](l) 
0
source

All Articles