Solved What are the / and * in function signatures? ELElaKowalskaOP 2 Oct, 13:56 edited Saw def f(a, b, /, c, *, d): in the docs. What do the slash and star mean?
FRFreyaL2 Oct, 20:53 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.
ZOZosiaN5 Oct, 08:17The * one is the useful one for your own code — it forces callers to write clear calls for flags.
AMAmeliaRuns3 Oct, 07:45/ was added in Python 3.8. You mostly see it in built-ins, e.g. help(len) shows len(obj, /).
Comments 4
Join the discussion CommentAlready have an account? Log in
- 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)andf(1, 2, c=3, d=4)work, butf(a=1, ...)doesn't.Huh, never seen the slash in tutorials.
The
*one is the useful one for your own code — it forces callers to write clear calls for flags./was added in Python 3.8. You mostly see it in built-ins, e.g.help(len)showslen(obj, /).