【发布时间】: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…