【问题标题】:What does the '...' operator do and why do all builtin class methods return it? [duplicate]'...' 运算符有什么作用,为什么所有内置类方法都返回它? [复制]
【发布时间】:2022-01-04 15:17:00
【问题描述】:

在内置的str 类中,所有方法都返回...。例如字符串类的第一位:

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: ...

这是什么意思,为什么他们返回它,而不是其他什么?

【问题讨论】:

  • 不就是为了省略其余的功能吗?
  • 似乎是反编译器的输出。这可能不是实际课程的样子。实际的类也将用 C 编写(str
  • 不是算子;它只是 ellipsis 类型值的文字。像任何其他值一样,它可以形成一个表达式,从而形成一个表达式语句,因此可以出现在任何预期任意语句的地方(例如形成函数的主体)。它通常用作占位符值。
  • 特别是关于骗子 PEP484 的答案
  • 您可能正在查看 IDE 的类型提示存根。实际的实现在别处,可能在 C…

标签: python built-in


【解决方案1】:

... 运算符有什么作用?

...Ellipsis literal,通常用于指示 something 应该放在一个空格中,但现在 nothing 在那里,尽管一些 3rd 方库例如NumpyFastAPI 将其用于他们的目的
另见What does the Ellipsis object do?

为什么所有的内置类方法都返回它?

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

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

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

但是,您可以阅读official docs 或使用内置的help() 命令来获取这些返回类型,所有这些都竭尽全力描述它们返回的内容,其中一些带有显式类型注释,例如@ 987654336@

>>> 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

源代码

【讨论】:

    猜你喜欢
    • 2020-07-21
    • 2017-10-26
    • 2012-10-03
    • 2011-08-19
    • 2015-01-07
    • 1970-01-01
    • 2018-01-03
    • 2013-01-09
    • 2023-03-03
    相关资源
    最近更新 更多