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.
Source
print(scale(4, clamp=True))Output
8Notes
/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
8
Execution time appears here after you run the example.