──────────────────────────────────────

14. Festival Workshop Shortlist · Collect Flexible Arguments

Collect extra values in a tuple and optional named details in a dictionary, then unpack both at the call.

Lesson files · lessons/callables/pack-and-unpack-arguments

Before you start
- Pass a value to a function by position or by parameter name.
- Read a tuple and loop over dictionary keys and values.

By the end
- Build a stock report from any supplied counts and tags, including an empty collection.
- Trace where the call unpacks the values and where the function collects them.

Where you can use it
Feed a stock-report helper several counts and whichever named details the team has recorded for that delivery.

In a definition, *args collects extra positional arguments into a tuple and **kwargs collects extra keyword arguments into a dictionary. You can name them for what they hold. In a call, *names passes the entries of a list as separate arguments and **details passes dictionary entries as keyword arguments. Use explicit parameter names for a fixed set of options. Flexible arguments suit a changing number of values or a changing set of detail names.

Predict
How many positional arguments does badge_lines receive?
- One list of names.
- Two names.

Worked example (example.py)

def badge_lines(*names, **details):
    lines = [f"Badges = {len(names)}"]
    for field, value in sorted(details.items()):
        lines.append(f"{field} = {value}")
    return lines


names = ["Sam", "Ari"]
details = {"team": "hosts", "room": "hall"}
for line in badge_lines(*names, **details):
    print(line)


Example result
Badges = 2
room = hall
team = hosts

Complete stock_report(*counts, **tags). Return lines starting with Total stock = N, followed by one key = value line per tag in key order. The starter reads space-separated counts on the first line and space-separated key=value tags on the second. A dash means that line supplies no arguments.

Starter program (starter.py)

def stock_report(*counts, **tags):
    return ["Total stock = 0"]


count_text = input().strip()
tag_text = input().strip()
counts = []
if count_text != "-":
    counts = [int(value) for value in count_text.split()]
tags = {}
if tag_text != "-":
    for tag in tag_text.split():
        key, value = tag.split("=", 1)
        tags[key] = value

for line in stock_report(*counts, **tags):
    print(line)


Starter input
3 2 0
team=repair shelf=A

Hint
Use sum(counts) for the total and loop over sorted(tags.items()) to build the remaining lines.

Case 1. Try the sample
Input
3 2 0
team=repair shelf=A
Expected output
Total stock = 5
shelf = A
team = repair

Case 2. Tags without counts
Input
-
team=helpers
Expected output
Total stock = 0
team = helpers

Case 3. Counts without tags
Input
7 1
-
Expected output
Total stock = 8

Stretch
Try a new tag such as condition=ready by changing only the input.

Reflect
Where in the program do the values become a tuple and a dictionary?

Check your prediction
Two names. The single star unpacks the list at the call. The single star in the definition collects those two values into a tuple.
