Pylerno
English

Flexible arguments

Recognize argument collection and expansion without overusing them.

On this page

The concept

In a definition, *args collects extra positional arguments into a tuple and **kwargs collects extra keyword arguments into a dictionary. The names args and kwargs are conventions; the stars provide the behavior. In a call, the same syntax expands a collection into arguments.

Flexible interfaces are appropriate for forwarding or genuinely variable inputs. They are less helpful when the operation naturally has two or three fixed inputs. Prefer explicit parameters when they communicate a clearer contract.

Worked example

python
def total(*numbers):
    return sum(numbers)


def show_profile(**details):
    for key, value in details.items():
        print(key, value)


def multiply(a, b):
    return a * b


print(total(1, 2, 3, 4))
values = [5, 6]
print(multiply(*values))
profile = {"name": "Dana", "city": "Duhok"}
show_profile(**profile)

Trace the result

  1. total receives several positional numbers as one tuple.
  2. multiply(*values) expands the two-item list into two arguments.
  3. show_profile(**profile) uses dictionary keys as argument names and displays their values.
text
10
30
name Dana
city Duhok
Line-by-line walkthrough

Lines 1–2: def total(*numbers):

Define the function total with input names , *numbers. Definition stores the operation rather than executing its body. An ordinary call executes a normal function; a coroutine runs when awaited/scheduled, and a generator advances when consumed. Decorators can provide a context-manager or wrapper interface. Indented statements below belong to this operation.

Line 2: return sum(numbers)

Evaluate sum(numbers) and return that value to the caller. This ends the current function call; subsequent statements on this execution path do not run.

Lines 5–7: def show_profile(**details):

Define the function show_profile with input names , **details. Definition stores the operation rather than executing its body. An ordinary call executes a normal function; a coroutine runs when awaited/scheduled, and a generator advances when consumed. Decorators can provide a context-manager or wrapper interface. Indented statements below belong to this operation.

Lines 6–7: for key, value in details.items():

Obtain each item from details.items() and bind it to (key, value) before executing the body. A tuple-shaped target unpacks that item. A loop else, if present, runs only when completion does not use break.

Line 7: print(key, value)

Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.

Lines 10–11: def multiply(a, b):

Define the function multiply with input names a, b. Definition stores the operation rather than executing its body. An ordinary call executes a normal function; a coroutine runs when awaited/scheduled, and a generator advances when consumed. Decorators can provide a context-manager or wrapper interface. Indented statements below belong to this operation.

Line 11: return a * b

Evaluate a * b and return that value to the caller. This ends the current function call; subsequent statements on this execution path do not run.

Line 14: print(total(1, 2, 3, 4))

Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.

Line 15: values = [5, 6]

Evaluate [5, 6], then bind or update values with the result. The chosen collection determines how later access, membership, and mutation behave. Assignment itself does not imply a deep copy.

Line 16: print(multiply(*values))

Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.

Line 17: profile = {"name": "Dana", "city": "Duhok"}

Evaluate {'name': 'Dana', 'city': 'Duhok'}, then bind or update profile with the result. The chosen collection determines how later access, membership, and mutation behave. Assignment itself does not imply a deep copy.

Line 18: show_profile(**profile)

Call show_profile with the displayed arguments. This call may have an effect even when its return value is not assigned.

Common mistake

Providing the same parameter twice, once positionally and once through expansion, raises TypeError. Keyword names must be strings.

Check your understanding

What does this program print?

python
def count(*items):
    return len(items)
print(count([1, 2]))
Choose one answer

Search documentation

Share this page