1

In the builtin str class, all of the methods return .... For example, the first bit of the string 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: ...

What does this mean, and why do they return it, rather than anything else?

Tom McLean
  • 5,583
  • 1
  • 11
  • 36
  • 2
    Isn't that only for omitting the rest of the function? – Pedro Maia Nov 26 '21 at 15:07
  • It can be used for slicing or as a replacement for pass. Check out this article. https://www.geeksforgeeks.org/what-is-three-dots-or-ellipsis-in-python3/ – anarchy Nov 26 '21 at 15:08
  • 1
    Seems like a decompiler output. That's probably not what the actual class looks like. Also the actual class would be written in C (for `str`) – rdas Nov 26 '21 at 15:08
  • 1
    It's not an operator; it's simply a literal for a value of type `ellipsis`. Like any other value, it can form an expression, and thus an expression statement, and thus stand anywhere an arbitrary statement is expected (such as to form the body of a function). It's *conventionally* used as a placeholder value. – chepner Nov 26 '21 at 15:09
  • 1
    Specifically the answers around PEP484 on the dupe – jonrsharpe Nov 26 '21 at 15:09
  • 1
    You're probably looking at the type hint stub of your IDE. The actual implementation is elsewhere, likely in C… – deceze Nov 26 '21 at 15:11
  • 1
    Your IDE is not showing you the _bodies_ of these functions (which, being built-in to Python, are not written in Python anyway). – davidbak Nov 26 '21 at 15:11
  • @davidbak My IDE is PyCharm – Tom McLean Nov 26 '21 at 15:26

1 Answers1

1

What does the ... operator do?

... is the 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 make special use of it for their purposes
See also What does the Ellipsis object do?

Why do all builtin class methods return it?

A few comments on your Question hint at what is happening; strings in Python (and most other languages unless they are very simple), are a special case of a Byte Array with an associated encoding (Strings are immutable sequences of Unicode code points.), and in CPython (there are other implementations of Python with various benefits and foibles, but CPython is by far the most popular), there is no native code that defines strings, and the implementation is written in C (hence C Python) .. there are many reasons for this, but practically, it's a lot faster, so this is done for most, if not all, builtins

Whatever means you're using to get at the class definitions (some IDE) likely can't discover the types because they're not available in Python code

This could be argued as a bug (perhaps with your IDE, though I don't believe CPython itself is exposing them clearly anywhere) .. however, I suspect the builtins are sufficiently special as to warrant not bothering, and always return an instance of themselves, another builtin, or None

However, you can read the official docs or use the builtin help() command to get at these return types, all of which go out of their way to describe what they return, some with an explicit type annotation such as 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.

On str itself

Source code

ti7
  • 16,375
  • 6
  • 40
  • 68