1

Whenever I see any python builtin function, I always find a function that has "...", for example:

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

But, I couldn't find the meaning of the "..."

Can anyone please explain what that means?

  • Does this answer your question? [What does the Ellipsis object do?](https://stackoverflow.com/questions/772124/what-does-the-ellipsis-object-do) – HTF Jul 21 '22 at 06:49
  • Its used when the return type is of type `Any`. see this link - https://www.geeksforgeeks.org/what-is-three-dots-or-ellipsis-in-python3/#:~:text=When%20the%20return%20value%20of%20the%20function%20is%20of%20type%3A%20Any%20%C2%A0 – Nick Jul 21 '22 at 06:51
  • This one is helpful, thanks! – Satyajeet Sahu Jul 21 '22 at 06:58

1 Answers1

4

... is the literal for the object named Elipsis, which used to exist only for special kinds of slicing (e.g. in numpy). It's a singleton object, like None and NotImplemented.

Because ... means you're eliding or leaving something out in normal writing, the literal sometimes gets used for that same purpose in Python code. If you're teaching somebody how to write a function, you might show them the def statement like this:

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.

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. 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. You can hint Callable[..., int] to refer to a value that is a callable object with unspecified parameters that returns an integer. 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).

Blckknght
  • 100,903
  • 11
  • 120
  • 169
  • The one thing I don't see here is what the `...` means in `def get_form(self, request, obj=..., change=..., **kwargs):` – boatcoder Nov 22 '22 at 16:09
  • 1
    @boatcoder: That's just an arbitrary use of the `Ellipsis` object as the default value for two arguments. It doesn't do anything special there. `None` would be a much more common default value. But maybe that function uses `None` to mean something specific that is not the default, so another sentinel object was needed, and the authors of the code decided on `...`. – Blckknght Nov 30 '22 at 07:31