斜杠在help()输出中意味着什么? [英] What does the slash mean in help() output?

查看:149
本文介绍了斜杠在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.

                                        ...

推荐答案

它表示

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.

函数的参数列表中的斜杠表示该函数之前的参数仅是位置参数.仅位置参数是没有外部可用名称的参数.调用仅接受位置参数的函数后,参数将仅根据其位置映射到参数.

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语言规范的一部分,从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()输出中意味着什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆