Search code examples
pythonpython-3.xpep570

What does Positional-only parameters do in Python 3.8?


What below python code does?

def pow(x, y, z=None, /):
    r = x**y
    if z is not None:
        r %= z
    return r

Solution

  • It's already define in Positional-only parameters in official documentation in Python 3.8.

    There is new syntax (/) to indicate that some function parameters must be specified positionally (i.e., cannot be used as keyword arguments). This is the same notation as shown by help() for functions implemented in C (produced by Larry Hastings’ “Argument Clinic” tool). Example:

    Now pow(2, 10) and pow(2, 10, 17) are valid calls, but pow(x=2, y=10) and pow(2, 10, z=17) are invalid.

    See PEP 570 for a full description.