gpt4 book ai didi

python - help() 列出方法签名时斜线是什么意思?

转载 作者:行者123 更新时间:2023-11-28 18:59:08 24 4
gpt4 key购买 nike

/ 在 Python 3.4 的 help 输出中对于右括号前的 range 是什么意思?

>>> help(range)
Help on class range in module builtins:

class range(object)
| range(stop) -> range object
| range(start, stop[, step]) -> range object
|
| Return a virtual sequence of numbers from start to stop by step.
|
| Methods defined here:
|
| __contains__(self, key, /)
| Return key in self.
|
| __eq__(self, value, /)
| Return self==value.

...

最佳答案

表示positional only parameters结束,您不能用作关键字参数的参数。在 Python 3.8 之前,此类参数只能在 C API 中指定。

意思是__contains__key参数只能按位置传入(range(5).__contains__(3)),不是作为关键字参数 (range(5).__contains__(key=3)),您可以在纯 python 函数中使用位置参数。

另见 Argument Clinic文档:

To mark all parameters as positional-only in Argument Clinic, add a / on a line by itself after the last parameter, indented the same as the parameter lines.

和(最近添加到)Python FAQ :

A slash in the argument list of a function denotes that the parameters prior to it are positional-only. Positional-only parameters are the ones without an externally-usable name. Upon calling a function that accepts positional-only parameters, arguments are mapped to parameters based solely on their position.

语法现在是 Python 语言规范的一部分,as of version 3.8 , 请参阅 PEP 570 – Python Positional-Only Parameters .在 PEP 570 之前,该语法已被保留以备将来可能包含在 Python 中,请参阅 PEP 457 - Syntax For Positional-Only Parameters .

仅位置参数可以使 API 更干净、更清晰,使其他纯 C 模块的纯 Python 实现更加一致且更易于维护,并且由于仅位置参数需要很少的处理,因此它们导致更快的 Python 代码.

关于python - help() 列出方法签名时斜线是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54763710/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com