When should I use positional-only, keyword-only, and ordinary parameters?
Quick answer
Use ordinary parameters when callers may provide a value either by position or by name. Choose positional-only when the parameter name should not be part of the calling interface, and keyword-only when naming an option makes calls clearer. In a function definition, / marks preceding parameters positional-only and * marks following parameters keyword-only. [S1]
The question, the details, the next step.
A practical checklist
Decide whether each parameter should be passed by position, name, or either.
Use
/when preceding parameters should be positional-only.Use
*when following parameters should be keyword-only.Leave parameters ordinary when both call styles are useful.
Review intended call forms before changing a signature.
A few more useful answers
Can an ordinary parameter be passed by position or name?
Yes. Ordinary parameters are positional-or-keyword. [S1]
Does `*` make every parameter keyword-only?
It makes the parameters that follow it keyword-only; parameters before it are not changed by that marker. [S1]
Does the syntax determine which style I should choose?
No. It defines the permitted call forms. Choose a style based on which names and positions you want callers to rely on.
Sources
References used for this guide.
- [S1] Python tutorial: positional-only, positional-or-keyword, and keyword-only parameters docs.python.org · Checked
