简体   繁体   English

如何在python的用户定义函数中实现“仅位置参数”?

[英]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)是无效的。

See PEP 570 for more details.有关更多详细信息,请参阅PEP 570

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 ParametersPython Positional Only Parameters 中阅读有关函数参数和仅位置参数类型的更多信息

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在给定参数字典的情况下使用仅位置参数调用函数? - How to call a function with positional-only parameters given an arguments dictionary? 如何将类型提示添加到 Python 中的仅位置参数和仅关键字参数? - How to add Type Hints to positional-only and keyword-only parameters in Python? Python 3.8 中带有仅位置参数的角落案例? - Corner case with positional-only parameters in Python 3.8? MyPy 在 venv 中使用其他 Pyhton 版本? (仅位置参数仅在 Python 3.8 及更高版本中受支持) - MyPy usingo other Pyhton version as in venv? (Positional-only parameters are only supported in Python 3.8 and greater) SystemError 在尝试在运行时创建带有位置参数的函数后引发 - SystemError is raised after attempt to create function with positional-only args in runtime inside other one 如何在Python中实现可选的位置参数 - How to implement optional positional parameter in Python discord.py 新超时 - 仅位置 arguments 错误 - discord.py new timeout - positional-only arguments error Python Pandas:用户定义的 function 缺少 1 个必需的位置参数 - Python Pandas: User defined function missing 1 required positional argument 如何将 function 的位置参数声明为 python 中的全局变量? - How to declare a positional parameter of a function as a global variable in python? 如何将参数传递给用户定义函数? - How to pass the parameter to User-Defined Function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM