help() 输出中的斜杠是什么意思? [英] What does the slash mean in help() output?

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

问题描述

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

<预><代码>>>>帮助(范围)关于模块内置类中的类范围的帮助:类范围(对象)|范围(停止)->范围对象|范围(开始,停止[,步骤])->范围对象||逐步返回从开始到停止的虚拟数字序列.||此处定义的方法:||__包含__(自我,键,/)|返回键在自己.||__eq__(自我,价值,/)|返回自我==值....

解决方案

它标志着仅定位参数,您不能用作关键字参数的参数.在 Python 3.8 之前,此类参数只能在 C API 中指定.

这意味着 __contains__key 参数只能按位置传入 (range(5).__contains__(3)),不是作为关键字参数 (range(5).__contains__(key=3)),你可以在纯 python 函数中用位置参数做的事情.

另请参阅 Argument Clinic 文档:

<块引用>

要在 Argument Clinic 中将所有参数标记为仅位置参数,请在最后一个参数后的一行中单独添加一个 /,缩进与参数行相同.

和(最近添加的)Python 常见问题:

<块引用>

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

语法现在是 Python 语言规范的一部分,作为3.8 版,参见 PEP 570 – Python Positional-Only 参数.在 PEP 570 之前,该语法已经保留以供将来可能包含在 Python 中,请参阅 PEP 457 -仅位置参数的语法.

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

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.

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.

Also see the Argument Clinic documentation:

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.

and the (very recent addition to) the 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.

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.

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天全站免登陆