What is Ellipsis in Python?

Asked

Viewed 1,007 times

12

In the list of native constants from Python, you can find Ellipsis.

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

In Python 3, there is still syntactic sugar ... that 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 the Ellipsis?
  • If possible, you can cite a functional example of its use?
  • 1

    "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)

  • 1

    @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 the pass (example).

2 answers

8


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.

  • 2

    One of the main uses is to omit indices in a dimension when accessing multidimensional matrices since you in Numpy. Since you wrote that damn answer, I’d say it’s worth taking an example of using Ellipsis on Numpy and putting in there as well.

0

Hi, I know the question is old, but I still think it is valid to add information. ... is used by the module pydantic to express attributes of a model as mandatory, but which can assume the value None. It is also used by the module fastapi to make mandatory the parameters of a function assuming the type value Query.

Example of pydantic:

from pydantic import BaseModel

class UserModel(BaseModel):
    username: str = Field(...)
    name: str = Field(...)
    email: str = Field(...)

Example of fastapi:

from fastapi import FastAPI, Query

app = FastAPI()

@app.get("/items/")
async def read_items(q: str = Query(..., min_length=3)):
    results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
    if q:
        results.update({"q": q})
    return results

Ellipsis fastapi documentation.

Pydantic documentation on Ellipsis.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.