Concept
Keyword-Only & Positional-Only Arguments (`*` and `/`)
Parameters before `/` MUST be positional. Parameters after `*` MUST be passed by keyword. This gives you bulletproof API boundaries.
python
def configure(mode, /, *, debug=False):
print(f"mode={mode}, debug={debug}")
configure("fast", debug=True) # Valid!
# configure(mode="fast") # TypeError: positional-only!
# 96. Keyword-Only & Positional-Only Arguments (`*` and `/`) Parameters before `/` MUST be positional. Parameters after `*` MUST be passed by keyword. This gives you bulletproof API boundaries: ```python def configure(mode, /, *, debug=False): print(f"mode={mode}, debug={debug}") configure("fast", debug=True) # Valid! # configure(mode="fast") # TypeError: positional-only! ``` Why does this matter? If you maintain a public library, keyword-only arguments prevent users from relying on parameter order, allowing you to reorder internal parameters without breaking their code.
Checkpoint
In `def f(*, key):`, how must `key` be passed by the caller?
Practice
Define a function `secure(*, token)` that returns `f"Authorized: {token}"`. Call it with `token="secret123"` and print the result.
01
Tap code blocks below to assemble
Available Blocks