本文介绍了斜杠在help()输出中意味着什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

/在右括号前在Python 3.4的help输出中对range的含义是什么?

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 文档:

Python常见问题解答:

语法现已成为Python语言规范的一部分,从3.8版开始,请参见 PEP 570 – Python Positional-仅参数 .在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,使原本仅C模式的模块的纯Python实现更加一致且更易于维护,并且由于仅位置参数需要很少的处理,因此它们可以导致更快的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.

这篇关于斜杠在help()输出中意味着什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 22:18