-
Notifications
You must be signed in to change notification settings - Fork 1
Python argument order
Here is a possible write up on the subject of argument order in python:
Python functions can accept different kinds of arguments, such as positional-only, positional-or-keyword, keyword-only, and variadic arguments. These kinds of arguments are distinguished by the Parameter.kind attribute, which can be one of the following values: POSITIONAL_ONLY, POSITIONAL_OR_KEYWORD, KEYWORD_ONLY, VAR_POSITIONAL, or VAR_KEYWORD.
The order of arguments in a function definition is important, as it determines how the arguments are bound to the parameters when the function is called. The general rule is that arguments of the same kind must be grouped together, and the order of the groups is:
- positional-only
- positional-or-keyword
- keyword-only
- *args (var-positional)
- **kwargs (var-keyword)
Within each group, arguments can be ordered arbitrarily, except that arguments with default values must follow arguments without default values. For example, the following function definition is valid:
def foo(a, b, /, c, d=1, *, e, f=2, **kwargs): pass
But the following one is not, because it violates the order of the groups and the default values:
def bar(a, /, b=1, c, *, d, e=2, f, **kwargs, *args): pass
The order of the arguments affects how the function can be called. Positional-only arguments can only be passed by position, positional-or-keyword arguments can be passed by position or by keyword, keyword-only arguments can only be passed by keyword, and variadic arguments can accept an arbitrary number of positional or keyword arguments. For example, given the foo function defined above, the following calls are valid:
foo(1, 2, 3, e=4)
foo(a=1, b=2, c=3, f=4, g=5)
foo(1, 2, 3, 4, 5, e=6, f=7, h=8)
But the following ones are not, because they either use the wrong kind of argument or omit a required argument:
foo(1, 2, e=3) # missing c
foo(1, b=2, c=3, e=4) # b cannot be passed by keyword
foo(1, 2, 3, d=4, 5, e=6) # cannot mix positional and keyword arguments
The order of the arguments also affects the Signature object returned by the inspect.signature () function, which represents the call signature of a callable object. The Signature object has a parameters attribute, which is an ordered mapping of parameter names to Parameter objects. The Parameter objects have attributes such as name, kind, default, and annotation, which correspond to the attributes of the arguments in the function definition.
The inspect module provides several functions and classes to help get information about the arguments and parameters of a callable object, such as getfullargspec
, getcallargs
, Signature.bind
, and Signature.bind_partial
. These functions and classes can handle different kinds of arguments and parameters, and support features such as annotations, default values, and positional-only parameters.
For more information about the order and kinds of arguments and parameters in Python, see the following sections of the current page: