Module 9 · Functions — topic 9 of 19
*args
Accepting any number of positional arguments with *args, how they arrive as a tuple, combining *args with normal parameters, and forwarding arguments to other functions.
Learning objectives
- Write functions that accept a variable number of arguments
- Combine *args with regular and keyword-only parameters
- Forward arguments with *args
Definition
A parameter written as *args collects all extra positional arguments of a call into a tuple named args (the name is a convention; the * is what matters).
Introduction
print() accepts any number of values. You can write such functions too: def total(*numbers) can be called as total(), total(5) or total(1, 2, 3, 4).
Why it matters
Variadic functions make APIs pleasant: max(3, 9, 4) instead of max([3, 9, 4]). They are also essential for wrappers and decorators that pass arguments through unchanged.
Detailed explanation
Inside the function, args is a tuple of the extra positional arguments — possibly empty.
Regular parameters before *args are filled first: def log(level, *messages).
Any parameter after *args can only be given by keyword: def join(*parts, sep=" ").
In a call, *sequence does the opposite: it unpacks a sequence into separate arguments. So f(*args) forwards whatever you received.
Use *args when the arguments are all the same kind of thing; if they have different meanings, use named parameters.
Syntax
- args is a tuple.
- Parameters after *args are keyword-only.
- f(*seq) unpacks when calling.
Code examples
Any number of values
tuple () 0 tuple (5,) 5 tuple (1, 2, 3, 4) 10
Regular, variadic and keyword-only
[INFO] server started on port 8080 [WARN] disk | 92%
More examples
Forwarding arguments
area(3, 4) -> 12 max(7, 2, 9, 4) -> 9 min(5, 1, 8) -> 1
Step-by-step explanation
- timed_call receives a function and packs the remaining arguments into the tuple args.
- func(*args) unpacks the tuple back into separate arguments.
- The f-string shows the tuple, which prints like a call's argument list.
- *[5, 1, 8] unpacks a list at the call site, so args is (5, 1, 8).
Expected output
Running “Forwarding arguments” prints exactly:
Practical applications
- Decorators and logging wrappers forward *args and **kwargs.
- Utility functions like os.path.join(*parts) accept any number of path parts.
Best practices
- Use *args only when the extra arguments are of the same kind.
- Give it a meaningful name: *numbers, *paths, *messages.
- Put optional settings after it as keyword-only parameters.
Common mistakes
Passing a list instead of unpacking
Avoid
Prefer
Without *, the whole list is one argument; unpack it to pass its items.
Expecting a list
Avoid
Prefer
args is a tuple, which is immutable.
Important notes
- The name args is only a convention; *values works the same way.
Key takeaways
- *args packs extra positional arguments into a tuple.
- Parameters after it are keyword-only.
- *seq in a call unpacks; use it to forward arguments.
Try it yourself
- 1Write average(*values) that returns None when called with no values.
- 2Write longest(*words) that returns the longest word, preferring the first in a tie.
Finished reading? Mark it complete to update your dashboard.
Suggested practice
1 question test this exact topic. Answers and explanations are revealed after you submit.
- ModerateWhat is printed?