A keyword-only parameter must be supplied by naming it rather than by position. In a function definition, place an asterisk before the keyword-only parameters: def configure(name, *, verbose=False). Call it with configure("job", verbose=True); configure("job", True) is invalid. Naming the parameter makes the call's intent clearer, especially when several optional settings exist. section 6.4.1
A built-in example
The list.sort method uses sort(*, key=None, reverse=False). The defaults allow numbers.sort() to use ordinary ascending order, while numbers.sort(reverse=True) requests descending order. Passing True positionally, as in numbers.sort(True), is invalid because reverse is keyword-only. section 6.1.1
Use keyword-only parameters when:
- Optional settings would be unclear when supplied only by position. section 6.4.1
- A constructor has more than a few positional arguments; keep important arguments positional and make additional ones keyword-only. section 8.1.2
- A variable-length positional parameter would otherwise consume arguments ambiguously. With def stringify_b(*items, item0), item0 must be written as a keyword, such as stringify_b(0, item0=1). section 6.4.2
numbers.sort(reverse=True) assert numbers == [7, 5, 4, 2]
Inspect a focused call that activates a keyword-only option on list.sort. section 6.1.1
