Skip to content

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.

Beginner 8 min read·Lesson 134 of 306 in Python Programming

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

Python
def total(*numbers):
return sum(numbers)
def log(level, *messages, sep=" "):
print(f"[{level}]", sep.join(map(str, messages)))
  • args is a tuple.
  • Parameters after *args are keyword-only.
  • f(*seq) unpacks when calling.

Code examples

Any number of values

Python
def total(*numbers):
print(type(numbers).__name__, numbers)
return sum(numbers)
print(total())
print(total(5))
print(total(1, 2, 3, 4))
Output
tuple ()
0
tuple (5,)
5
tuple (1, 2, 3, 4)
10

Regular, variadic and keyword-only

Python
def log(level, *messages, sep=" "):
return f"[{level}] " + sep.join(str(m) for m in messages)
print(log("INFO", "server", "started", "on port", 8080))
print(log("WARN", "disk", "92%", sep=" | "))
Output
[INFO] server started on port 8080
[WARN] disk | 92%

More examples

Forwarding arguments

Python
def timed_call(func, *args):
result = func(*args) # pass everything through unchanged
return f"{func.__name__}{args} -> {result}"
def area(w, h):
return w * h
print(timed_call(area, 3, 4))
print(timed_call(max, 7, 2, 9, 4))
print(timed_call(min, *[5, 1, 8]))
Output
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:

TextOutput
area(3, 4) -> 12
max(7, 2, 9, 4) -> 9
min(5, 1, 8) -> 1

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

total([1, 2, 3]) # numbers == ([1, 2, 3],) -> sum fails

Prefer

total(*[1, 2, 3])

Without *, the whole list is one argument; unpack it to pass its items.

Expecting a list

Avoid

def f(*args):
args.append(1)

Prefer

def f(*args):
items = list(args)
items.append(1)

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

  1. 1
    Write average(*values) that returns None when called with no values.
  2. 2
    Write 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?
Practise this topic