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.
source share