What do the / and * markers in a Python function signature mean?
Parameters before / are positional-only, so callers cannot pass them by name. Parameters after a bare * are keyword-only, so callers must pass them by name. Both make an API clearer and safer to change.
Explain it simply
At a canteen counter, some things you order by position in the queue ("first, second") and some by name ("extra spicy: yes"). In a signature, / ends the by-position-only section and * starts the by-name-only section.
Anything between the two can be passed either way.
Example
def bill(amount, /, tax, *, discount=0):
return amount + tax - discount
print(bill(100, 18)) # Output: 118
print(bill(100, tax=18, discount=5)) # Output: 113
try:
bill(amount=100, tax=18)
except TypeError:
print("amount is positional-only") # Output: amount is positional-only
try:
bill(100, 18, 5)
except TypeError:
print("discount is keyword-only") # Output: discount is keyword-onlyPositional-only parameters (Python 3.8+) let a library rename a parameter later without breaking callers. Keyword-only parameters stop mistakes like passing a flag as the third positional value.
What to say in the interview
"Before / the parameters are positional-only; after * they are keyword-only; in between they are normal. Keyword-only arguments make calls readable, like timeout=5, and positional-only ones let me change parameter names freely. The / syntax needs Python 3.8 or later."
Common mistakes
- Putting
/after a*, or giving it twice; both are SyntaxErrors.