简体   繁体   English

python中的“...”是什么意思?

[英]What is the meaning of "..." in python?

Whenever I see any python builtin function, I always find a function that has "...", for example:每当我看到任何 python 内置函数时,我总是会找到一个带有“...”的函数,例如:

def append(self, __object: _T) -> None: ...

But, I couldn't find the meaning of the "..."但是,我找不到“……”的意思。

Can anyone please explain what that means?谁能解释一下这是什么意思?

... is the literal for the object named Elipsis , which used to exist only for special kinds of slicing (eg in numpy ). ...是名为Elipsis的对象的字面量,它曾经只存在于特殊类型的切片中(例如在numpy中)。 It's a singleton object, like None and NotImplemented .它是一个单例对象,例如NoneNotImplemented

Because ... means you're eliding or leaving something out in normal writing, the literal sometimes gets used for that same purpose in Python code.因为...意味着您在正常写作中省略或遗漏了某些内容,所以文字有时会在 Python 代码中用于相同的目的。 If you're teaching somebody how to write a function, you might show them the def statement like this:如果你正在教某人如何编写函数,你可以像这样向他们展示def语句:

def foo(bar):
    ...

Here the ... doesn't do anything useful, it just shows where you left out the body of the function.在这里...没有做任何有用的事情,它只是显示您遗漏了函数体的位置。 The more traditional way of doing nothing is the pass statement, but ... seems to be taking over, perhaps especially for when the function should have a body, you're just not showing it.更传统的什么都不做的方式是pass语句,但是...似乎正在接管,也许特别是当函数应该有一个主体时,你只是没有显示它。

This usage became common among users of type hinting.这种用法在类型提示的用户中很常见。 It is often necessary to write "stub" functions that show the types of arguments expected by functions written in third part libraries when their authors didn't use type hinting.当作者不使用类型提示时,通常需要编写“存根”函数来显示第三方库中编写的函数所期望的参数类型。 Stubs don't need a full function body, so ... gets put instead.存根不需要完整的函数体,所以...被放置。

Since type hinting was introduced as an optional feature of Python, the interpreter didn't include type hints in its own code for the standard library, so stubs files had to be created for all of the functions and methods of the builtins and the standard library.由于类型提示是作为 Python 的一个可选特性引入的,解释器在其标准库的代码中没有包含类型提示,因此必须为内置函数和标准库的所有函数和方法创建存根文件. A lot of the features of stub formatting has been reused in various bits of documentation, since it concisely shows what types are expected by a function.存根格式化的许多功能已在各种文档中重复使用,因为它简洁地显示了函数期望的类型。

There's only one specific situation where ... has a special meaning in type hinting, that's for typing.Callable .只有一种特定情况...在类型提示中具有特殊含义,即用于typing.Callable You can hint Callable[..., int] to refer to a value that is a callable object with unspecified parameters that returns an integer.您可以提示Callable[..., int]引用一个值,该值是具有未指定参数的可调用对象,该参数返回一个整数。 This is only done because it's often awkward to hint all of a callable's arguments properly, especially for a function that can be called a bunch of different ways.这样做只是因为正确暗示所有可调用的参数通常很尴尬,尤其是对于可以以多种不同方式调用的函数。 There's been talk about adding new syntax for hinting Callable more directly, but it hasn't been nailed down yet ( PEP 677 proposed (int, str) -> bool to mean Callable[[int, str], bool] , but it was rejected last December).一直在谈论添加新语法以更直接地提示Callable ,但尚未确定( PEP 677提议(int, str) -> bool表示Callable[[int, str], bool] ,但它是去年12月被拒绝)。

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

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