Defining position-only parameters with the / separator
In Python 3.8, an additional annotation was added to function definitions. We can use the /
character in the parameter list to separate the parameters into two groups. Before /
, all parameters work positionally, or names may not be used with argument values. After /
, parameters may be given in order, or names may be used.
This should be used for functions where the following conditions are all true:
- A few positional parameters are used (no more than three)
- They are all required
- The order is so obvious that any change might be confusing
This has always been a feature of the standard library. As an example, the math.sin()
function can only use positional parameters. The formal definition is as follows:
>>> help(math.sin)
Help on built-in function sin in module math:
sin(x, /)
Return the sine of x (measured in radians).
Even though there's an x
parameter name, we can...