Solved

What are the / and * in function signatures?

ElaKowalskaOP edited

Saw def f(a, b, /, c, *, d): in the docs. What do the slash and star mean?

0

Comments 4

Join the discussion

Already have an account? Log in

  1. FreyaL Solutionreplies: 2

    - Everything before / is positional-only: you can't pass it by name.
    - Everything after * is keyword-only: you must pass it by name.
    - In between: either way.

    So f(1, 2, 3, d=4) and f(1, 2, c=3, d=4) work, but f(a=1, ...) doesn't.

    3
      1. ZosiaN

        The * one is the useful one for your own code — it forces callers to write clear calls for flags.

        5
    1. AmeliaRuns

      / was added in Python 3.8. You mostly see it in built-ins, e.g. help(len) shows len(obj, /).

      1