0

def func (*args)

How to make a code that combains all elements in args

6th Jan 2020, 12:00 PM
Pattern
Pattern - avatar
4 RĂ©ponses
+ 2
If you have a function definition: def func (*args): ... all elements passed to the function are contained within a list variable called args. So simply def func(*args): return sum(args) print(func(2,3,5,7)) # 17 ...can be done. But I have to ask; if all elements come from an input, why not just put the inputs straight into a list and find the sum?
6th Jan 2020, 2:21 PM
Russ
Russ - avatar
+ 1
I think you'll find a good explanation here https://stackoverflow.com/questions/3394835/use-of-args-and-kwargs
7th Jan 2020, 7:45 PM
Alexey Rubtsov
Alexey Rubtsov - avatar
0
What do you mean by "combines all elements"?
6th Jan 2020, 1:26 PM
Russ
Russ - avatar
0
Simply a function returns a+b+c +....... I don't know how many arguments in func cause they will be an input so the user will input random some values how to make a code that combains all of them
6th Jan 2020, 1:45 PM
Pattern
Pattern - avatar