0

I'm trying to learn and work with *args in functions. I see and understand how the *args works here:

def sum(*args): #pack the received positional args into data structure of tuple. after applying '*' - def sum((1,2,3,4))
    sum = 0
    for a in args:
        sum+=a
    print(sum)

Output:

sum(1,2,3,4)  #positional args sent to function sum
10

What I can't understand is how to (without changing the function) pass in the argumets if they are stored within a variable. How can I store 1, 2, 3, 4 as a variable to pass in to get the same output? For example the following won't work as it stores as a tuple (or likewise variable_values = [1,2,3,4] stored as a list. These will give the errors:

TypeError: unsupported operand type(s) for +=: 'int' and 'tuple'

and

TypeError: unsupported operand type(s) for +=: 'int' and 'list'

So how do you get this to work with variables as an example below?

variable_values = 1,2,3,4
sum(variable_values)
chitown88
  • 24,774
  • 3
  • 26
  • 56

0 Answers0