views:

4862

answers:

3

This came up in Hidden features of Python, but I can't see good documentation or examples that explain how the feature works.

+20  A: 

You'd use it in your own class, since no builtin class makes use of it.

Numpy uses it, as stated in the documentation. Some examples here.

In your own class, you'd use it like this:

>>> class TestEllipsis(object):
...     def __getitem__(self, item):
...         if item is Ellipsis:
...             return "Returning all items"
...         else:
...             return "return %r items" % item
... 
>>> x = TestEllipsis()
>>> print x[2]
return 2 items
>>> print x[...]
Returning all items

Of course, there is the python documentation, and language reference. But those aren't very helpful.

nosklo
looks quite brokensince the "propper" way to say all items is >>> x[:] >>> x[:, 1:2]
Ronny
@Ronny: The point was to demonstrate some custom usage of Ellipsis.
nosklo
@Ronny and since the colon creates a copy when used on an array, a custom type might also return a copy when colon is used. `x = arr[:]` `x is arr == False`. `x = your_instance[...]`, maybe `x is your_instance == True`
DGGenuine
+41  A: 

The ellipsis is used 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)

Now, you have a 4-dimensional matrix of order 2x2x2x2. To select all first elements in the 4th dimension, you can use the ellipsis notation

>>> a[..., 0].flatten()
array([ 0,  2,  4,  6,  8, 10, 12, 14])

which is equivalent to

>>> a[:,:,:,0].flatten()
array([ 0,  2,  4,  6,  8, 10, 12, 14])

In your own implementations, you're free to ignore the contract mentioned above and use it for whatever you see fit.

Torsten Marek
+18  A: 

This is another use for Ellipsis, which has nothing to do with slices: I often use it in intra-thread communication with queues, as a mark that signals "Done"; it's there, it's an object, it's a singleton, and its name means "lack of", and it's not the overused None (which could be put in a queue as part of normal data flow). YMMV.

P.S: I don't mind downvotes, when what I say in an answer is not useful in relation to the question; then I try to improve my answer. But I sure can't understand how one can downvote any of the answers in this question— when the question is “how do you use the Ellipsis in Python”… It seems that people think that downvoting means “I disagree” or “I don't like this”.

ΤΖΩΤΖΙΟΥ
Never thought of that, nice idea!
Torsten Marek
Mightn't it be clearer to just say: "Done = object()" somewhere and just use that?
Brandon Craig Rhodes
Not necessarily - it requires you to actually _say_ Done=object() somewhere. Sentinel values aren't necessarily a bad thing -- and using otherwise nearly-useless Python singletons as sentinels isn't so horrible IMO (Ellipsis and () are the ones I've used where None would be confusing).
Rick Copeland