Functions

Positional-only Parameters

Use / to mark parameters that callers must pass by position.

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
12
A bare / divides positional-only arguments from positional-or-keyword ones; callers cannot name a or b.
In the same signature, the bare * works the other way: parameters after it, such as clamp, must be named at the call site.

Parameters 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

10

The 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

TypeError

Notes

See also

Run the complete example

Example code

Expected output

8
12
10
TypeError

Execution time appears here after you run the example.