Star
The
* makes all
following parameters keyword-only.
If F were tried to be called so: F(1,2,3,4), Python would raise TypeError: F() takes 2 positional arguments but 4 were given.
def F(a, b, *, c, d=100):
print(f'{a=}')
print(f'{b=}')
print(f'{c=}')
print(f'{d=}')
F(1,2,c=3) # d does not need to be specified because it has a default value
F(1,2,c=3, d=4)
F(1,2,d=4, c=3)
Slash
The
/ makes all
preceding parameters positional-only.
If F were tried to be called so: F(a=1, b=2, c=3), Python would raise TypeError: F() got some positional-only arguments passed as keyword arguments: 'a, b'.
def F(a, b, /, c, d=100):
print(f'{a=}')
print(f'{b=}')
print(f'{c=}')
print(f'{d=}')
F(1, 2, 3) # c is positional; d uses its default
F(1, 2, 3, d=4)
F(1, 2, c=3, d=4)
Using both, the star and the slash
def F(a, b, /, c, *, d, e=100):
print(f'{a=}, {b=}, {c=}, {d=}, {e=}')
For
F, the parameters
-
a and b are positional-only.
-
c can be positional or keyword.
-
d and e are keyword-only.
F may be called with variations of
F(1, 2, 3, d=4)
F(1, 2, c=3, d=4, e=5)
The following calls are invalid and raise TypeError: F() got some positional-only arguments passed as keyword arguments: 'a, b'
F(a=1, b=2, c=3, d=4)
F(1, 2, 3, 4)