Via arbitrary arguments a function can receive a dynamic count of arguments.
They're specified by adding a *
prefix.
Let's look at the following example:
Output:
Multiple arguments are passed to printData
but all of them are accessed
only by the single *numbers
argument.
At line 2
the len
function is used to get the count of the passed arguments.
At lines 4 and 5
the passed arguments are accessed via indexes starting from 0.
For such arguments the order doesn't matter and they can be set via "key = value" syntax.
Let's look at the following example:
Output:
The order of the passed arguments is not the same as the arguments order of the function definition.
age = 20
sets value of 20
to the age
argument.
Suitable if the count of keyword arguments is unknown.
They're specified by adding a **
prefix.
Example:
Output:
The values for the passed arguments is stored inside data
.
data["make"]
accesses the passed value for make
which is "Toyota"
.