Python: Keyword Only Parameters (*)

By Xah Lee. Date: .

you can define function that allows only keywords arguments in function call.

add asterisk (*) in a position in the parameter. anything after that must be keyword arguments only.

def ff(a, b, *, c):
    return [a, b, c]

# here, c must be passed as keyword argument
print(ff(3, 4, c=5))
# [3, 4, 5]
def ff(a, b, *, c):
    return [a, b, c]

print(ff(3, 4, 5))
    # print(ff(3, 4, 5))
          # ~~^^^^^^^^^
# TypeError: ff() takes 2 positional arguments but 3 were given