September 2026
1051 pages
35h 4m
English
You can also define parameters that may be passed exclusively as keyword arguments. Such keyword-only parameters are separated by an asterisk (*) from the positional arguments:
>>> def f(*, c, d):... print(c, d) >>> def g(a, b, *, c, d):... print(a, b, c, d)
In this case, the function f expects the keyword-only parameters c and d. In contrast to that, the function g expects in addition the positional parameters a and b. For both functions, there is no way to pass the c and d parameters except as keyword arguments:
>>> f(1, 2)Traceback (most recent call last): File "<python-input-1>", line 1, in <module> f(1, 2) ~^^^^^^TypeError: f() takes 0 positional arguments but 2 were given>>> f(c=1, d=2)1 2>>> g(1, 2,
Read now
Unlock full access