Positional-only 参数在 Python 3.8 中有什么作用?
What does Positional-only parameters do in Python 3.8?
下面的 python 代码是做什么的?
def pow(x, y, z=None, /):
r = x**y
if z is not None:
r %= z
return r
调用函数时,不能用关键字参数指定 positional-only 参数的值。 pow(1, 2, 3)
会起作用; pow(x=1, y=2, z=3)
不会。
PEP-0570 中对其进行了很好的描述。如果禁止对标记为 positional-only 的参数使用命名参数:
>>> pow(x=5, y=3)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: pow() takes no keyword arguments
你只能这样称呼它 pow(5, 3)
。
在Python3.8的官方文档Positional-only parameters中已经定义了。
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.
有关完整说明,请参阅 PEP 570。
下面的 python 代码是做什么的?
def pow(x, y, z=None, /):
r = x**y
if z is not None:
r %= z
return r
调用函数时,不能用关键字参数指定 positional-only 参数的值。 pow(1, 2, 3)
会起作用; pow(x=1, y=2, z=3)
不会。
PEP-0570 中对其进行了很好的描述。如果禁止对标记为 positional-only 的参数使用命名参数:
>>> pow(x=5, y=3)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: pow() takes no keyword arguments
你只能这样称呼它 pow(5, 3)
。
在Python3.8的官方文档Positional-only parameters中已经定义了。
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.
有关完整说明,请参阅 PEP 570。