-3

What below python code does?

def pow(x, y, z=None, /):
    r = x**y
    if z is not None:
        r %= z
    return r
ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
  • 1
    have you tried running it? – static const Jun 27 '19 at 16:33
  • You can read [PEP 570](https://www.python.org/dev/peps/pep-0570/) for a full description, but essentially the parameters before `/` cannot be passed by name (e.g. you cannot do `pow(x=1, y=2)` or `pow(y=2, z=3, x=1)`), but instead they must be passed by position, in the order specified by the signature. – jdehesa Jun 27 '19 at 16:38
  • It does the same thing as an ordinary function in earlier versions. It just prevents you from calling the function with parameters in a different order. – Barmar Jun 27 '19 at 16:38

3 Answers3

0

You cannot specify the value of a positional-only paramter with a keyword argument when you call the function. pow(1, 2, 3) will work; pow(x=1, y=2, z=3) will not.

chepner
  • 497,756
  • 71
  • 530
  • 681
0

It is pretty well described in PEP-0570. If forbids using named parameters for parameters marked as positional-only:

>>> pow(x=5, y=3)
Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
TypeError: pow() takes no keyword arguments

You can only call it like pow(5, 3).

Paulo Scardine
  • 73,447
  • 11
  • 124
  • 153
0

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.

Community
  • 1
  • 1
BestTester
  • 16
  • 1