Positional-only (/) and keyword-only (*) parameters
def f(a, b, /, c, d, *, e, f): everything before / MUST be passed positionally; everything after *
MUST be passed by keyword; c, d in the middle can be either. This is how stdlib functions like len()
lock down their signature (you can't call len(obj=x)), and it's increasingly used in library code to keep
parameter names as an implementation detail that can change without breaking callers.
def f(a, b, /, c, d, *, e, f):
return (a, b, c, d, e, f)
print(f(1, 2, 3, d=4, e=5, f=6))
print(f(1, 2, c=3, d=4, e=5, f=6))
try:
f(a=1, b=2, c=3, d=4, e=5, f=6) # a, b are positional-only
except TypeError as ex:
print("TypeError:", ex)
try:
f(1, 2, 3, 4, 5, 6) # e, f are keyword-only
except TypeError as ex:
print("TypeError:", ex)