简体   繁体   English

内置列表function说明中的“/”是什么

[英]What is the " / " in the built-in list function descriptions

At the end of the built-in list function descriptions is "/".内置列表 function 描述的末尾是“/”。 What is it?它是什么?

>>> l=[]
>>> help(l.insert)
Help on built-in function insert:

insert(index, object, /) method of builtins.list instance
    Insert object before index.

>>> help(l.index)
Help on built-in function index:

index(value, start=0, stop=9223372036854775807, /) method of builtins.list instance
    Return first index of value.

From Python documentation about help() :来自Python 有关 help() 的文档

Note that if a slash(/) appears in the parameter list of a function, when invoking help(), it means that the parameters prior to the slash are positional-only.请注意,如果斜线(/)出现在 function 的参数列表中,则在调用 help() 时,这意味着斜线之前的参数是仅位置参数。

and from the FAQ entry on positional-only parameters :以及来自positional-only parameters 的 FAQ 条目

What does the slash(/) in the parameter list of a function mean?一个function的参数列表中的斜杠(/)是什么意思?

A slash in the argument list of a function denotes that the parameters prior to it are positional-only. function 的参数列表中的斜杠表示它之前的参数仅是位置参数。 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. For example, divmod() is a function that accepts positional-only parameters.在调用接受仅位置参数的 function 时,arguments 将映射到仅基于其 position 的参数。例如,divmod() 是接受仅位置参数的 function。 Its documentation looks like this:它的文档如下所示:

>>> help(divmod)
Help on built-in function divmod in module builtins:
>
> divmod(x, y, /)
>    Return the tuple (x//y, x%y).  Invariant: div*y + mod == x.

The slash at the end of the parameter list means that both parameters are positional-only.参数列表末尾的斜杠表示两个参数都是位置参数。 Thus, calling divmod() with keyword arguments would lead to an error:因此,使用关键字 arguments 调用 divmod() 会导致错误:

>>> divmod(x=3, y=4)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: divmod() takes no keyword arguments

There are three kinds of parameters in Python: Python中有3种参数:

  1. Positional-only, which can only be set by positional arguments Positional-only,只能通过positional arguments设置
  2. "Regular", which can be set either by positional arguments or keyword arguments “常规”,可以通过位置 arguments 或关键字 arguments 设置
  3. Keyword-only, which can only be set by keyword arguments. Keyword-only,只能通过关键字arguments设置。

When defining a function, a / is used to separate positional-only parameters (at least one) on the left and the rest on the right.在定义 function 时,使用/分隔左侧的仅位置参数(至少一个)和右侧的 rest。 If there is no / in the parameter list, there are no positional-only parameters.如果参数列表中没有/ ,则没有位置参数。

The syntax originated in the argument clinic, which is used to define functions for the CPython implementation.语法起源于参数 clinic,它用于为 CPython 实现定义函数。 Its use appeared in help for such functions before it was added to the syntax of Python itself in PEP-570.在 PEP-570 中将其添加到 Python 本身的语法之前,它的使用出现在此类函数的帮助中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM