04 / 05

How do positional-only (/) and keyword-only (*) parameter markers refine function signatures?

Difficulty: 5/10
Positional-Only, Keyword-Only, Function Signatures, PEP 570

/ makes preceding parameters positional-only; * makes following parameters keyword-only

In a function signature, the / marker (PEP 570, Python 3.8+) indicates that all parameters before it are positional-only: callers cannot pass them by keyword. This is useful when the parameter name is an implementation detail that you may want to change later without breaking callers, or when the name would conflict with a keyword. The * marker indicates that all parameters after it are keyword-only: callers must pass them by name. This is useful for optional flags and configuration parameters because it makes call sites self-documenting and prevents accidental positional argument mistakes. You can combine both: def f(a, b, /, c, *, d): a and b are positional-only, c can be positional or keyword, and d is keyword-only. These markers are part of the function signature and are enforced at call time with TypeError. They are a design tool for public APIs and for avoiding ambiguity in functions with many parameters.

  1. 1

    def f(a, b, /): a and b must be passed positionally; f(a=1, b=2) raises TypeError.

  2. 2

    def f(*, a, b): a and b must be passed by keyword; f(1, 2) raises TypeError.

  3. 3

    Combined: def f(pos_only, /, normal, *, kw_only): mixes all three kinds.

  4. 4

    *args collects extra positional arguments; **kwargs collects extra keyword arguments.

  5. 5

    Trade-off: stricter signatures prevent misuse but can break callers if you change the marker later. Use them deliberately for public APIs.

  6. 6

    Common mistake: adding / or * after parameters that existing callers pass by keyword, breaking compatibility.

  7. 7

    Common mistake: confusing * as multiplication with * in a signature; the meaning depends on context.

  8. 8

    Version note: / was introduced in Python 3.8 (PEP 570). * for keyword-only parameters has been available since Python 3.0.

javascript

Scenario Questions

0-2 years experience

  1. 1How do you define a function where all arguments must be passed by keyword?
  2. 2What does the / in a function signature mean?

2-5 years experience

  1. 1You have a function with many optional flags. How do you force callers to use keywords?
  2. 2You want to rename a parameter without breaking callers. Which marker helps and why?

5-8 years experience

  1. 1You are designing a public library API. How do you decide which parameters should be positional-only, keyword-only, or normal?
  2. 2You add / to an existing function and a caller breaks. How do you handle the migration?

8+ years experience

  1. 1Design a function signature for a complex configuration API that is backward-compatible, self-documenting, and resistant to positional mistakes.
  2. 2Explain how PEP 570 and keyword-only markers interact with typing, introspection, and functools.partial in a library context.

Follow-up Questions

  • When would you use positional-only parameters in a public API?
  • How do these markers interact with *args and **kwargs?
Share

Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.