[英]How to implement "positional-only parameter" in a user defined function in python?
How can I implement "positional-only parameter" for a function that is user defined in python?如何为用户在 python 中定义的函数实现“仅位置参数”?
def fun(a, b, /):
print(a**b)
fun(5,2) # 25
fun(a=5, b=2) # should show error
Before Python 3.8, the /
syntax was only documentational.在 Python 3.8 之前,
/
语法只是文档性的。 Starting from 3.8 , you can use it for specifying positional-only parameters in function definitions.从3.8开始,您可以使用它在函数定义中指定仅位置参数。 Example:
例子:
def pow(x, y, z=None, /):
r = x**y
if z is not None:
r %= z
return r
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.现在
pow(2, 10)
和pow(2, 10, 17)
是有效的调用,但是pow(x=2, y=10)
和pow(2, 10, z=17)
是无效的。
Update: this answer will become increasingly out-dated;更新:这个答案会越来越过时; please see https://stackoverflow.com/a/56149093/1126841 instead.
请参阅https://stackoverflow.com/a/56149093/1126841 。
The only solution would be to use a *
-parameter, like so:唯一的解决方案是使用
*
参数,如下所示:
def fun(*args):
print(args[0] ** args[1])
But this comes with its own problems: you can't guaranteed the caller will provide exactly two arguments;但这有其自身的问题:您不能保证调用者将提供恰好两个参数; your function has to be prepared to handle 0 or 1 arguments.
您的函数必须准备好处理 0 或 1 个参数。 (It's easy enough to ignore extra arguments, so I won't belabor the point.)
(忽略额外的参数很容易,所以我不会详细说明这一点。)
Positional only arguments feature was added in python 3.8 after PEP 570 was accepted, before it was found in documentation in function signature to indicate function doesn't take any keyword arguments.在接受PEP 570之后,python 3.8 中添加了仅位置参数功能,然后在函数签名的文档中找到它以指示函数不接受任何关键字参数。
Parameters in function definition prior Forward slash (/) are positional only and parameters followed by slash(/) can be of any kind as per syntax.
函数定义之前的参数在正斜杠 (/) 之前只是位置参数,斜杠 (/) 后面的参数可以是任何类型的语法。 Where arguments are mapped to positional only parameters solely based on their position upon calling a function.
其中参数仅根据调用函数时的位置映射到仅位置参数。 Passing positional-only parameters by keywords(name) is invalid.
通过关键字(名称)传递仅位置参数无效。
Following example show function definition with positional only parameters以下示例显示仅具有位置参数的函数定义
def foo(a, b, / , x, y):
print("positional ", a, b)
print("positional or keyword", x, y)
In the above function definition parameter a and b have to be passed as positional only arguments while x or y can be either positional or keyword.在上面的函数定义中,参数 a 和 b 必须作为仅位置参数传递,而 x 或 y 可以是位置参数或关键字。
Following function calls are valid以下函数调用有效
foo(40, 20, 99, 39)
foo(40, 3.14, "hello", y="world")
foo(1.45, 3.14, x="hello", y="world")
Following function call will raise an exception TypeError since a and be are passed as keyword arguments.以下函数调用将引发异常 TypeError,因为 a 和 be 作为关键字参数传递。
foo(a=1.45, b=3.14, x=1, y=4)
TypeError: foo() got some positional-only arguments passed as keyword arguments: 'a, b'
类型错误:foo() 得到了一些作为关键字参数传递的仅位置参数:'a, b'
It is possible to define a function to accept positional only argument by adding forward slash (/) as last parameter in function definition.通过在函数定义中添加正斜杠 (/) 作为最后一个参数,可以将函数定义为仅接受位置参数。
def pow(x, y, /):
return x ** y
In the above function definition all parameter (x and y) are positional only.在上面的函数定义中,所有参数(x 和 y)都是位置参数。 Here, Passing arguments by their name not valid which will result in TypeError.
在这里,按名称传递参数无效,这将导致 TypeError。
pow(2, 3); pow(3, 9)
pow(2, 3); pow(3, 9)
are valid while pow(x=3, y=9)
is invalid which will raise TypeError pow(2, 3); pow(3, 9)
有效而pow(x=3, y=9)
无效,这将引发 TypeError
Read more about types of function arguments and positional-only parameters at Python Positional Only Parameters在Python Positional Only Parameters 中阅读有关函数参数和仅位置参数类型的更多信息
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.