*args/**kwargs: collection vs unpacking, and ordering rules
*args/**kwargs in a function definition COLLECT extra arguments; the same syntax at a call site
UNPACKS a sequence/mapping into separate arguments. A call can mix positional, *iterable, keyword, and
**mapping unpacking, but Python enforces a strict order, and duplicate keyword collisions raise
immediately rather than silently picking one.
def show(*args, **kwargs):
print("args:", args, "kwargs:", kwargs)
show(1, 2, 3, x=4, y=5)
nums = [1, 2, 3]
opts = {"x": 4, "y": 5}
show(*nums, **opts) # unpack at call site
show(0, *nums, z=9, **opts) # mixing positional, unpack, keyword, unpack
try:
d1 = {"x": 1}
d2 = {"x": 2}
show(**d1, **d2) # duplicate key across two unpacked dicts
except TypeError as ex:
print("TypeError:", ex)