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 used mainly in conjunction with the syntax of Slice extended to user-defined container data types.
I don’t know any examples used in pure code. I usually use Ellipsis
when I write doctests
. First, let’s look at a code, it’ll 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 reading this as: "The answer of this function will start with Olá
.
Now going back to the definition is a continuation of slice
. Starting from the point that "Hello Anderson" has 12 characters. And we only passed the "Hello" (slice of [0:3]) is as if the validation of the eternal was done using the beginning of the string knowing that something is expected in the end.
Then the ...
, in this case it would be a delimiting to assert the beginning of the value produced (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 assertion anywhere in any iterable without describing it completely, starting from the point of an interval.
So in that case (doctests) We can simulate any output without having to be very judicious in thinking about the result, because any response would be sufficient. So think of the scope of docstrings every return obtained by an object is the method __repr__
and the idea behind the ellipsis
is to make the assertive within what is returned by the representation of the object.
Something like:
class Anderson:
pass
The class Anderson
has no representation as it does not implement the method __repr__
, then your print
would be something like <__main__.Anderson object at 0x7fa28656c5c0>
, but for each execution the value 0x7fa28656c5c0
will not be the same, and to make this validation we could use <__main__.Anderson object at ...>
and we would be sure of the result because it can be validated without taking into account the address where the class was allocated.
"Ellipsis" comes from the Greek, meaning "omission". It is used as a type of missing/omitted information, in counterpoint to the parable (the perfect information, neither left nor missing) and "hyperbole" (information remaining that would not be necessary in that context)
– Jefferson Quesado
@Jeffersonquesado em esse filosofia, vi que as vezes a Ellipsis é utilizado no lugar do
pass
to omit the definition of a function:def foo(): ...
. This results in a valid expression and leaves more explicit than something it was omitted that the use of thepass
(example).– Woss