Any number of arguments
print takes any number of arguments: print("a"), print("a", "b", "c"), even print(). Your own functions can do the same. A star * before a parameter's name collects all the remaining arguments of a call into a tuple:
def total(*numbers):
print("Got", len(numbers), "numbers:", numbers)
return sum(numbers)
print(total(1, 2, 3))
print(total(10))
print(total())
Got 3 numbers: (1, 2, 3)
6
Got 1 numbers: (10,)
10
Got 0 numbers: ()
0Inside the function, numbers is an ordinary tuple: you can loop over it, index it and take its length. (A tuple with one item is printed with a comma, (10,), to tell it apart from a number in parentheses.) When no better name fits, such a parameter is usually called *args, short for arguments.
With ordinary parameters
Ordinary parameters can come before the starred one. They take the first arguments, and the starred one gets all the rest, which may be none at all:
def report(title, *scores):
print(title)
for score in scores:
print("-", score)
report("Round 1", 7, 9, 4)
report("Round 2")
Round 1
- 7
- 9
- 4
Round 2A list is one argument
A list given to such a function is still one argument, not many. Here numbers becomes a tuple with one item, the list, and sum can't add a list to a number:
def total(*numbers):
return sum(numbers)
values = [1, 2, 3]
print(total(values))
Traceback (most recent call last):
File "main.py", line 6, in <module>
print(total(values))
~~~~~^^^^^^^^
File "main.py", line 2, in total
return sum(numbers)
TypeError: unsupported operand type(s) for +: 'int' and 'list'A star in the call spreads the list out into separate arguments: total(*values) is the same as total(1, 2, 3), and returns 6.