'...' 运算符的作用是什么?为什么所有内置 class 方法 return 都这样做?

What does the '...' operator do and why do all builtin class methods return it?

在内置 str class 中,所有方法 return ...。比如字符串的第一位class:

class str(Sequence[str]):
    @overload
    def __new__(cls: Type[_T], o: object = ...) -> _T: ...
    @overload
    def __new__(cls: Type[_T], o: bytes, encoding: str = ..., errors: str = ...) -> _T: ...
    def capitalize(self) -> str: ...
    def casefold(self) -> str: ...
    def center(self, __width: int, __fillchar: str = ...) -> str: ...
    def count(self, x: str, __start: Optional[SupportsIndex] = ..., __end: Optional[SupportsIndex] = ...) -> int: ...
    def encode(self, encoding: str = ..., errors: str = ...) -> bytes: ...
    def endswith(
        self, __suffix: Union[str, Tuple[str, ...]], __start: Optional[SupportsIndex] = ..., __end: Optional[SupportsIndex] = ...
    ) -> bool: ...
    def expandtabs(self, tabsize: int = ...) -> str: ...
    def find(self, __sub: str, __start: Optional[SupportsIndex] = ..., __end: Optional[SupportsIndex] = ...) -> int: ...
    def format(self, *args: object, **kwargs: object) -> str: ...
    def format_map(self, map: _FormatMapMapping) -> str: ...
    def index(self, __sub: str, __start: Optional[SupportsIndex] = ..., __end: Optional[SupportsIndex] = ...) -> int: ...
    def isalnum(self) -> bool: ...
    def isalpha(self) -> bool: ...
    if sys.version_info >= (3, 7):
        def isascii(self) -> bool: ...
    def isdecimal(self) -> bool: ...
    def isdigit(self) -> bool: ...
    def isidentifier(self) -> bool: ...
    def islower(self) -> bool: ...
    def isnumeric(self) -> bool: ...
    def isprintable(self) -> bool: ...
    def isspace(self) -> bool: ...
    def istitle(self) -> bool: ...
    def isupper(self) -> bool: ...
    def join(self, __iterable: Iterable[str]) -> str: ...
    def ljust(self, __width: int, __fillchar: str = ...) -> str: ...
    def lower(self) -> str: ...
    def lstrip(self, __chars: Optional[str] = ...) -> str: ...

这是什么意思,为什么他们 return 它而不是其他任何东西?

... 运算符的作用是什么?

...Ellipsis literal, which is commonly used to indicate something should go in a space, but nothing is there now, though some 3rd party libraries such as Numpy and FastAPI为了自己的目的而特意利用的
另见 What does the Ellipsis object do?

为什么所有内置 class 方法都 return 呢?

; Python(以及大多数其他语言,除非它们非常简单)中的字符串是具有关联编码的字节数组的特例(字符串是 Unicode 代码点的不可变序列。),并且在 CPython 中(Python 的其他实现具有各种优点和缺点,但 CPython 是迄今为止最受欢迎的),有 没有定义字符串的本机代码,并且实现是用 C 编写的(因此 C Python)..这有很多原因,但实际上,它快很多,所以这是为大多数(如果不是全部)内置函数完成的

无论您使用什么方式获取 class 定义(某些 IDE),都可能无法发现类型,因为它们在 Python 代码中不可用

这可以说是一个错误(可能是你的 IDE,虽然我不相信 CPython 本身会在任何地方清楚地暴露它们)..然而,我怀疑内置函数是足够特别以保证不打扰,并且总是 return 它们自己的一个实例,另一个内置的,或 None

但是,您可以阅读 official docs 或使用内置 help() 命令来获取这些 return 类型,所有这些类型都特意描述了它们 return,有些带有显式类型注释,例如 str.find(...)

>>> help(str)
Help on class str in module builtins:

class str(object)
 |  str(object='') -> str
 |  str(bytes_or_buffer[, encoding[, errors]]) -> str
...
 |  find(...)
 |      S.find(sub[, start[, end]]) -> int
 |      
 |      Return the lowest index in S where substring sub is found,
 |      such that sub is contained within S[start:end].  Optional
 |      arguments start and end are interpreted as in slice notation.
 |      
 |      Return -1 on failure.

str本身

源代码