Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python 3: Ellipsis in function parameters?

Tags:

python-3.x

I recently ran into Ellipsis (...) that is used in function parameters in aiohttp code and then in that function's body:

def make_mocked_request(method, path, headers=None, *,
                        match_info=sentinel,
                        version=HttpVersion(1, 1), closing=False,
                        app=None,
                        writer=sentinel,
                        protocol=sentinel,
                        transport=sentinel,
                        payload=sentinel,
                        sslcontext=None,
                        client_max_size=1024**2,
                        loop=...):
    """Creates mocked web.Request testing purposes.

    Useful in unit tests, when spinning full web server is overkill or
    specific conditions and errors are hard to trigger.

    """

    task = mock.Mock()
    if loop is ...:
        loop = mock.Mock()
        loop.create_future.return_value = ()

Can you explain this new python 3 feature?

like image 354
Boris Burkov Avatar asked Jan 28 '19 16:01

Boris Burkov


People also ask

What do 3 dots mean in Python?

Ellipsis notation[…] is used as a default secondary prompt in Python interpreter which is seen during multi-line constructs.

What does dot dot dot do in Python?

When you use dot notation, you indicate to Python that you want to either run a particular operation on, or to access a particular property of, an object type. Python knows how to infer the object type on which this operation is being run because you use dot notation on an object.

What is ellipsis in numpy?

The ellipsis is used in numpy to slice higher-dimensional data structures. It's designed to mean at this point, insert as many full slices ( : ) to extend the multi-dimensional slice to all dimensions. Example: >>> from numpy import arange >>> a = arange(16).reshape(2,2,2,2)


1 Answers

Ellipsis is a Built-in constant in Python. In Python 3 it has a literal syntax ... so it can be used like any other literal. This was accepted by Guido for Python 3 because some folks thought it would be cute.

The code you have found (use as function argument default) is apparently one such "cute" usage. Later in that code you'll see:

if loop is ...:
    loop = mock.Mock()
    loop.create_future.return_value = ()

It's just being used as a sentinel, here, and may as well be object() or anything else - there is nothing specific to Ellipsis. Perhaps the usual sentinel, None, has some other specific meaning in this context, although I can not see any evidence of that in the commit (it seems like None will have worked just as well).

Another use-case for an ellipsis literal sometimes seen in the wild is a placeholder for code not written yet, similar to pass statement:

class Todo:
    ...

For the more typical use-cases, which are involving the extended slice syntax, see What does the Python Ellipsis object do?

like image 110
wim Avatar answered Sep 22 '22 08:09

wim