What is Ellipsis in Python?

In Python's list of native constants, one can find Ellipsis.

print(Ellipsis, type(Ellipsis)) #-> (Ellipsis, <type 'ellipsis'>)

In Python 3, there is still the syntactic sugar ... which represents the constant Ellipsis.

print(..., type(...)) #-> (Ellipsis, <type 'ellipsis'>)
print(... is Ellipsis) #-> True

So,

  • What is the function of the constant Ellipsis?
  • What problems can be solved using Ellipsis?
  • if possible, can you cite a functional example of its use?
Author: Woss, 2017-11-01

1 answers

In the official documentation , you will find something like:

The same as .... Special value used mostly in conjunction with extended slicing syntax for user-defined container data types.

Freely translating:

The same as .... Special value mainly used in conjunction with the extended slice syntax for user-defined container data types.

I don't I know of no examples used in pure code. Generally use Ellipsis when I write doctests. Before, let's look at a code, it will make more sense before the explanation:

def test() -> None:
    """
    Diz olá ao Anderson.

    >>> test()
    Olá ...
    """
    print("Olá Anderson")

if __name__ == "__main__":
    import doctest
    doctest.testmod(verbose=True, optionflags=doctest.ELLIPSIS)

Taking a look at docstring Olá ..., you can try to read this as: "the response of this function will start with Olá.

Now returning the definition is a continuation of slice. Starting from the point that "Hello Anderson" has 12 characters. And we only pass the "Hello" (slice of [0: 3]) it is as if the validation of the iterable was done using the start of the string knowing that something is expected at the end.

Then ..., in this case it would be a delimitation so that the assert of the beginning of the produced value is made (starts with "Hello" and goes '...').

Another legal use would be not to use ELLIPSIS to delimit the end, plus the middle or at the beginning: O...n or ... Anderson. So you could make an assertive in any part of any iterable without describing the same completely, starting from the point of an interval.

So in this case (doctests ) we can simulate any output without terms that be very judicious when thinking about the result, since any answer would be sufficient. So think that in the scope of docstrings every return obtained by an object is the method __repr__ and the idea behind ellipsis is to make the assertive within what is returned by the representation of the object.

Something like:

class Anderson:
   pass

The class Anderson no it has representation, because it does not implement the method __repr__, so its print would be something like <__main__.Anderson object at 0x7fa28656c5c0>, but each execution the value 0x7fa28656c5c0 will not be the same, and to do this validation we could use <__main__.Anderson object at ...> and we would be sure of the result because the same can be validated without taking into account the address where

 7
Author: Eduardo Mendes, 2017-11-01 13:38:57