CKTIS / Python
Variadic parameters: *args and **kwargs
The *args parameter collects excess positional arguments into a tuple, while **kwargs collects excess keyword arguments into a dictionary. They let a function accept a variable number of arguments.
Version: Python 3.14; repository image, production runtime not attested
Syntax
def function_name(*args, **kwargs):
...In a function definition, *args collects positional arguments not assigned to preceding parameters into a tuple. **kwargs collects unmatched keyword arguments into a dictionary. With no such arguments, they receive () and {}, respectively. The names args and kwargs are conventions; * and ** provide the behavior.
You can use either parameter or both. **kwargs must be the last parameter. Ordinary parameters after *args, such as active in the example, are keyword-only. If you supply a value for such a parameter, you must pass it by name. If the parameter has a default value, you can omit it from the call. A bare * also introduces keyword-only parameters but does not collect positional arguments.
In the example, label receives the first argument, args collects 10 and 20, active receives True, and kwargs contains only color. In the second call, args and kwargs are empty, and active is omitted, so it uses its default value, False. Do not confuse collection in a definition with unpacking *values and **options in a call: there, the operators pass elements as separate arguments.
Reference identifiers covered by this entry: *args / **kwargs parameters.
Examples
def collect(label, *args, active=False, **kwargs):
print(label, args, active, kwargs)
collect("A", 10, 20, active=True, color="blue")
collect("B")Result
A (10, 20) True {'color': 'blue'}
B () False {}The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.