First, a parameter is a named entity in the function/method definition that specifies an argument. An argument is a value passed to a function.
For example,
def rectangle_area(height, width):
pass
rectangle_area(argument_1, argument_2)
height, width
are the function parameters, and argument_1, argument_2
are the arguments passed to the function. When you say positional argument, you are talking about arguments, this has nothing to do with the function definition. width
and height
are (by default in Python) positional parameters or keyword parameters (so called positional-or-keyword parameters). Therefore, you could pass arguments either positionally or by keywords.
How you are calling/passing the value to the function determines if they are positional arguments or keyword arguments.
For the function rectangle_area
we could call it equally like so:
rectangle_area(1, 2) # positional arguments
rectangle_area(width=2, height=1) # keyword arguments
- In the first calling, we pass values positionally: 1 is passed to the height and 2 to the width. That is, Python infers that when you say
1, 2
we mean height is 1 and width is 2 based on the position they are passed (i.e., in the function definition the first parameter is the height
and the second is the width
).
- In the second calling, we pass values by keywords. We are hinting to Python which parameter we are passing the argument to. In the second example, we flip the order of the arguments, yet we tell Python that height is still 1 and width is still 2. Both callings have exactly the same result.
positional-only and keyword-only
The thing not many people know is that you can specify a positional-only parameter by using the /
in the parameter list (example from here).
def func(positional_only1, positional_only2, /, positional_or_keyword): ...
Similarly, you can also have keyword-only parameters by using the *
character.
def func(positional_or_keyword, *, keyword_only1, keyword_only2): ...
Finally, we also have var-positional and var-keyword (a.k.a *args and **kwargs respectively). Meaning, you can have arbitrary sequence of positional arguments or keyword arguments passed to the function.