More on How Function Arguments Work in Python
Learn about positional-only parameters and keyword-only arguments.
We'll cover the following...
Positional-only parameters
Positional arguments (variable or not) are those that are provided first to functions in Python. The values for these arguments are interpreted based on the position they're provided to the function, meaning they're assigned respectively to the parameters in the function's definition.
If we don't make use of any special syntax when defining the function arguments, by default, they can be passed by position or keyword. For example, in the following function, all calls to the function are equivalent:
def my_function(x, y):print(f"{x=}, {y=}")my_function(1, 2)my_function(x=1, y=2)my_function(y=2, x=1)my_function(1, y=2)
This means, in the first case, we pass the values 1
and 2
, and by their position, they're assigned to the parameters x
and y
, respectively. With this syntax, nothing stops us from passing the same arguments with their keyword (even in reverse order), should that be needed (for example, to be more explicit). The only constraint here is that if we pass one argument as a keyword, all the following ones must be provided as a keyword as well (the last example wouldn't work with the parameters reversed). ...