1

Many builtin and standard library functions in python have their function signature ending in \

Example

>>> help(divmod)
Help on built-in function divmod in module builtins:

divmod(x, y, /)
    Return the tuple (x//y, x%y).  Invariant: div*y + mod == x.
def my_func(arg, /):
    pass

print(repr(inspect.signature(my_func)))
# Output: <Signature (arg, /)>

What does the / here imply and why is it used?

FalseDev
  • 464
  • 1
  • 4
  • 12

1 Answers1

3

/ was added in python 3.8 and implies that all the arguments preceding it are positional only and cannot be provided in kwargs method as shown in this document: https://docs.python.org/3/whatsnew/3.8.html#positional-only-parameters

There is a new function parameter syntax / to indicate that some function parameters must be specified positionally and cannot be used as keyword arguments. This is the same notation shown by help() for C functions annotated with Larry Hastings’ Argument Clinic tool.

In the following example, parameters a and b are positional-only, while c or d can be positional or keyword, and e or f are required to be keywords:

def f(a, b, /, c, d, *, e, f):
    print(a, b, c, d, e, f)

The following is a valid call:

f(10, 20, 30, d=40, e=50, f=60)

However, these are invalid calls:

f(10, b=20, c=30, d=40, e=50, f=60)   # b cannot be a keyword argument
f(10, 20, 30, 40, 50, f=60)           # e must be a keyword argument
AmaanK
  • 1,032
  • 5
  • 25