help() 输出中的斜线是什么意思?
问题描述
在 Python 3.4 的 help
输出中,/
在右括号之前是什么意思?
What does the /
mean in Python 3.4's help
output for range
before the closing parenthesis?
>>> 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.
...
解决方案
它表示 仅位置参数,您不能用作关键字参数的参数.在 Python 3.8 之前,此类参数只能在 C API 中指定.
It signifies the end of the positional only parameters, parameters you cannot use as keyword parameters. Before Python 3.8, such parameters could only be specified in the C API.
表示__contains__
的key
参数只能按位置传入(range(5).__contains__(3)
),不是作为关键字参数 (range(5).__contains__(key=3)
),您可以在纯 Python 函数中使用位置参数.
It means the key
argument to __contains__
can only be passed in by position (range(5).__contains__(3)
), not as a keyword argument (range(5).__contains__(key=3)
), something you can do with positional arguments in pure-python functions.
另请参阅 Argument Clinic 文档:
要在 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 常见问题解答:
函数参数列表中的斜杠表示它之前的参数是仅位置参数.仅位置参数是没有外部可用名称的参数.在调用接受仅位置参数的函数时,参数仅根据其位置映射到参数.
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版本 3.8,请参阅 PEP 570 - Python Positional-Only 参数.在 PEP 570 之前,该语法已被保留以备将来可能包含在 Python 中,请参阅 PEP 457 -仅位置参数的语法.
The syntax is now part of the Python language specification, as of version 3.8, see PEP 570 – Python Positional-Only Parameters. Before PEP 570, the syntax was already reserved for possible future inclusion in Python, see PEP 457 - Syntax For Positional-Only Parameters.
仅位置参数可以使 API 更简洁明了,使纯 Python 实现的其他纯 C 模块更加一致且更易于维护,并且由于仅位置参数需要很少的处理,它们会导致更快的 Python 代码.
Positional-only parameters can lead to cleaner and clearer APIs, make pure-Python implementations of otherwise C-only modules more consistent and easier to maintain, and because positional-only parameters require very little processing, they lead to faster Python code.
相关文章