Python:help() 输出中的斜杠是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24735311/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 05:06:13  来源:igfitidea点击:

Python: What does the slash mean in help() output?

pythonpython-3.xintrospection

提问by Joschua

What does the /mean in Python 3.4's helpoutput for rangebefore the closing parenthesis?

右括号之前/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.

                                        ...

采纳答案by Martijn Pieters

It signifies the end of the positional onlyparameters, parameters you cannotuse as keyword parameters. Such parameters can only be specified in the C API.

它象征着结束位置唯一参数,参数,你不能作为关键字参数使用。此类参数只能在 C API 中指定。

It means the keyargument 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 cando with positional arguments in pure-python functions.

这意味着keyto的参数__contains__只能通过位置 ( range(5).__contains__(3))传递,而不是作为关键字参数 ( range(5).__contains__(key=3))传递,您可以在纯 Python 函数中使用位置参数实现这一点。

Also see the Argument Clinicdocumentation:

另请参阅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.

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

and the (very recent addition to) the Python FAQ:

以及(最近添加的)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.

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

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.

Positional-only 参数可以带来更清晰、更清晰的 API,使其他纯 C-only 模块的纯 Python 实现更加一致和更容易维护,并且因为 positional-only 参数需要很少的处理,它们导致更快的 Python 代码。

回答by prosti

I asked this question myself. :) Found out that /was originally proposed by Guido in here.

我自己问过这个问题。:) 发现/最初是由 Guido 在此处提出的。

Alternative proposal: how about using '/' ? It's kind of the opposite of '*' which means "keyword argument", and '/' is not a new character.

替代建议:如何使用 '/' ?它有点像 '*' 意思是“关键字参数”,而 '/' 不是一个新字符。

Then his proposal won.

然后他的提议赢了

Heh. If that's true, my '/' proposal wins:

 def foo(pos_only, /, pos_or_kw, *, kw_only): ...

呵呵。如果这是真的,我的“/”提案将获胜:

 def foo(pos_only, /, pos_or_kw, *, kw_only): ...


I think the very relevant document covering this is PEP 570. Where recap section looks nice.

我认为涵盖此内容的非常相关的文件是PEP 570。回顾部分看起来不错的地方。

Recap

The use case will determine which parameters to use in the function definition:

 def f(pos1, pos2, /, pos_or_kwd, *, kwd1, kwd2):

As guidance:

Use positional-only if names do not matter or have no meaning, and there are only a few arguments which will always be passed in the same order. Use keyword-only when names have meaning and the function definition is more understandable by being explicit with names.

回顾

用例将确定在函数定义中使用哪些参数:

 def f(pos1, pos2, /, pos_or_kwd, *, kwd1, kwd2):

作为指导:

如果名称无关紧要或没有意义,并且只有少数参数始终以相同的顺序传递,请使用仅位置。当名称有意义并且函数定义通过明确的名称更易于理解时,仅使用关键字。



If the function ends with /

如果函数以 /

def foo(p1, p2, /)

This means all functional arguments are positional.

这意味着所有函数参数都是位置参数。

回答by neotam

Forward Slash (/) indicates all arguments prior to it are positional only argument. Positional only arguments feature was added in python 3.8 after PEP 570was accepted. Initially this notation was defined in PEP 457 - Notation for Notation For Positional-Only Parameters

正斜杠 (/) 表示它之前的所有参数都是位置参数。在接受PEP 570后,python 3.8 中添加了仅位置参数功能。最初,这个符号是在PEP 457 - Notation for Notation For Positional-Only Parameters 中定义的

Parameters in function definition prior Foraward slash (/) are positional only and parameters followed by slash(/) can be of any kind as per syntax. Where arguments are mapped to positional only parameters solely based on their position upon calling a function. Passing positional-only parameters by keywords(name) is invalid.

Foraward 斜杠 (/) 之前的函数定义中的参数只是位置参数,斜杠 (/) 后面的参数可以是任何类型的语法。其中参数仅根据调用函数时的位置映射到仅位置参数。通过关键字(名称)传递仅位置参数无效。

Let's take following example

让我们以下面的例子

def foo(a, b, / , x, y):
   print("positional ", a, b)
   print("positional or keyword", x, y)

Here in the above function definition parameters a and b are positional-only, while x or y can be either positional or keyword.

在上面的函数定义中,参数 a 和 b 仅是位置参数,而 x 或 y 可以是位置参数或关键字。

Following function calls are valid

以下函数调用有效

foo(40, 20, 99, 39)
foo(40, 3.14, "hello", y="world")
foo(1.45, 3.14, x="hello", y="world")

But, following function call is not valid which raises an exception TypeError since a, b are not passed as positional arguments instead passed as keyword

但是,以下函数调用无效,这会引发异常 TypeError,因为 a、b 不是作为位置参数传递而是作为关键字传递

foo(a=1.45, b=3.14, x=1, y=4)

TypeError: foo() got some positional-only arguments passed as keyword arguments: 'a, b'

类型错误:foo() 得到了一些作为关键字参数传递的仅位置参数:'a, b'

Many built in function in python accept positional only arguments where passing arguments by keyword doesn't make sense. For example built-in function lenaccepts only one positional(only) argument, Where calling len as len(obj="hello world") impairs readability, check help(len).

python 中的许多内置函数只接受位置参数,其中通过关键字传递参数没有意义。例如内置函数len只接受一个 positional(only) 参数,其中调用 len as len(obj="hello world") 会降低可读性,请检查 help(len)。

>>> help(len)
Help on built-in function len in module builtins:

len(obj, /)
    Return the number of items in a container.

Positional only parameters make underlying c/library functions easy to maintain. It allows parameters names of positional only parameters to be changes in future without risk of breaking client code that uses API

仅位置参数使底层 c/库函数易于维护。它允许将来更改仅位置参数的参数名称,而不会破坏使用 API 的客户端代码

Last but not least, positional only parameters allow us to use their names to be used in variable length keyword arguments. Check following example

最后但并非最不重要的一点是,仅位置参数允许我们在可变长度关键字参数中使用它们的名称。检查以下示例

>>> def f(a, b, /, **kwargs):
...     print(a, b, kwargs)
...
>>> f(10, 20, a=1, b=2, c=3)         # a and b are used in two ways
10 20 {'a': 1, 'b': 2, 'c': 3}

Positional only parameters is better Explained here at Types of function arguments in python: Positional Only Parameters

仅位置参数更好在 Python 中的函数参数类型中解释:仅位置参数

Positional-only parameters syntax was officially added to python3.8. Checkout what's new python3.8 - positional only arguments

仅位置参数语法正式添加到python3.8。查看python3.8 的新功能 - 仅位置参数

PEP Related: PEP 570 -- Python Positional-Only Parameters

PEP 相关:PEP 570 -- Python Positional-Only 参数