1. Home
  2. Interview Questions
  3. Python
  4. 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

0 0
InterviewQuestions Team ✔ Expert answer ✔ Best 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: *args collects any extra positional arguments into a tuple.
  • Keyword-only: everything after * or *args. The caller must pass these by name.
  • Var-keyword: **kwargs collects 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 like def f(obj, /, **kwargs) can still accept obj=... inside kwargs. This matters for wrappers and dict-like APIs.
  • Matching builtins. Many C builtins already behave this way. len(obj=x) fails, and help(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 in def f(a, *):. This is a SyntaxError because 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.signature to 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

All hard questions →

Related Questions

‹
Q7 of 10 · Python
Next ›

Interview Questions

Free interview questions and answers, MCQ practice, online tests, mock interviews and group discussions — everything you need to crack your next interview.
📍 Dasardot LLC
1309 Coffeen Avenue STE 1200
Sheridan, Wyoming 82801
USA