- Home
- Interview Questions
- Python
- How do positional-only (/) and keyword-only (*) parameters w…
Try answering first
In a real interview nobody shows you the answer. Think it through (or type it), then reveal the answers and rate yourself.
1 Answer
Short answer: Parameters before a / can only be passed positionally. Parameters after a bare *, or after *args, can only be passed by keyword. Library authors use them to control how a function may be called, so they can rename parameters safely and force clear call sites.
Detailed explanation
A full Python signature has five parameter kinds, always in this order:
def f(pos_only, /, normal, *args, kw_only, **kwargs): ...- Positional-only: everything before
/. Added in Python 3.8 by PEP 570. The caller cannot pass these by name. - Positional-or-keyword: the normal parameters between
/and*. - Var-positional:
*argscollects any extra positional arguments into a tuple. - Keyword-only: everything after
*or*args. The caller must pass these by name. - Var-keyword:
**kwargscollects any extra keyword arguments into a dict.
Why use keyword-only parameters? They make call sites readable and stop callers from passing values in the wrong order. Compare send(msg, True, False) with send(msg, retry=True, urgent=False). Keyword-only parameters can also be required with no default, even when they come after parameters that do have defaults. That is not allowed for positional parameters.
Why use positional-only parameters?
- Freedom to rename. Callers can never write the name, so you can rename the parameter later without breaking anyone's code.
- No clash with
**kwargs. A function likedef f(obj, /, **kwargs)can still acceptobj=...inside kwargs. This matters for wrappers anddict-like APIs. - Matching builtins. Many C builtins already behave this way.
len(obj=x)fails, andhelp(len)shows the/.
The kinds are visible at runtime through inspect.signature(f).parameters. Each Parameter.kind is one of POSITIONAL_ONLY, POSITIONAL_OR_KEYWORD, VAR_POSITIONAL, KEYWORD_ONLY or VAR_KEYWORD.
Example
def connect(host, port=5432, /, *, timeout=10, ssl):
return f'{host}:{port} timeout={timeout} ssl={ssl}'
connect('db', 5433, ssl=True) # OK
connect('db', ssl=True, timeout=30) # OK, port uses its default
connect(host='db', ssl=True)
# TypeError: connect() got some positional-only arguments
# passed as keyword arguments: 'host'
connect('db', 5433, 30, ssl=True)
# TypeError: connect() takes from 1 to 2 positional arguments
# but 3 were given
connect('db')
# TypeError: connect() missing 1 required keyword-only argument: 'ssl'
# Positional-only lets kwargs reuse the same name
def build(name, /, **attrs):
return name, attrs
build('div', name='header') # ('div', {'name': 'header'})Note that ssl is required even though it comes after timeout=10. That is legal only because both are keyword-only.
Common mistakes
- Writing a bare
*with nothing after it, as indef f(a, *):. This is aSyntaxErrorbecause named arguments must follow a bare*. - Putting
/after*, or otherwise breaking the fixed order. The positional-only section must come first. - Forgetting the default ordering rule. Among positional parameters, once one has a default, all later ones before
*need defaults too. Keyword-only parameters are exempt from this rule. - Confusing
*in a signature with unpacking at a call site.f(*items)spreads an iterable into positional arguments. It has nothing to do with marking parameters keyword-only.
Follow-up questions
- How would you write a decorator whose wrapper accepts any arguments without risking a name clash with the wrapped function's parameters?
- How can you use
inspect.signatureto check which parameters of a function are keyword-only? - Before Python 3.8, how did people approximate positional-only parameters in pure Python code?
Interview tip: Write the five-part signature template from memory and give one concrete reason for each marker: "/ lets me rename safely, * forces readable calls". That shows API-design thinking, not just syntax recall.
Reviewed by the Interview Questions editorial team · Last updated
How well did you know this?
Rate yourself to track progress, build your streak and get “didn't know” questions in your revision list.
Your Answer
More hard Python questions
Related Questions
- Why can you call a function declaration before it is defined but not a function expression? · JavaScript · 1 ans
- Why does using a mutable default argument like a list cause bugs in Python? · Python · 1 ans
- What is broadcasting in NumPy and what rules decide whether two arrays can be combined? · Python · 1 ans
- Why does Pandas raise SettingWithCopyWarning and how do you fix chained assignment correctly? · Python · 1 ans
- What is the difference between assignment, shallow copy and deep copy in Python? · Python · 1 ans
- How do you write a Python decorator that accepts its own arguments? · Python · 1 ans
- What is exception chaining in Python and when should you use raise from? · Python · 1 ans
- In what order do try, except, else and finally blocks run in Python? · Python · 1 ans
🙋 Was this asked in your interview?
Which company? (optional — adds this question to the company's page)