Python: Positional Only Parameters (/)

By Xah Lee. Date: .

you can define function with only positional parameters, to disallow keywords arguments in function call.

add slash in a position in the parameter. anything before that must be positional argument only.

ff(aa, bb, /)

# function with positional only parameters
def ff(x, y, /):
    return [x, y]

# in function call, using positional args.
print(ff(3, 4))
# [3, 4]

# using keyword arg is error
# print(ff(x=3, y=4))
    # print(ff(x=3, y=4))
          # ~~^^^^^^^^^^
# TypeError: ff() got some positional-only arguments passed as keyword arguments: 'x, y'
# anything before slash must be positional argument in function call
def ff(x, y, /, z):
    return [x, y, z]

# using keyword arg for z
print(ff(3, 4, z=5))
# [3, 4, 5]