Functions
Positional-only Parameters
Parameters before / are positional-only. value is the main input, while factor remains an ordinary parameter that can be named.
Source
def scale(value, /, factor=2, *, clamp=False):
result = value * factor
if clamp:
result = min(result, 10)
return result
print(scale(4))
print(scale(4, factor=3))Output
8
12Parameters after * are keyword-only. That makes options such as clamp explicit at the call site — here 4 * 3 would be 12, and the clamp visibly caps the result at 10.
Source
print(scale(4, factor=3, clamp=True))Output
10The restriction is enforced, not advisory: passing the positional-only value by keyword raises TypeError at the call site.
Source
try:
scale(value=4)
except TypeError as error:
print(type(error).__name__)Output
TypeErrorNotes
/marks parameters before it as positional-only.*marks parameters after it as keyword-only.- Use these markers when the call shape is part of the API design.
See also
- contrast: Keyword-only Arguments
- related: Functions
- related: Args and Kwargs
Run the complete example
Expected output
8
12
10
TypeError
Execution time appears here after you run the example.