Ellipsis is used mainly by the numeric python extension, which adds a multidimensional array type. Since there are more than one dimensions, slicing becomes more complex than just a start and stop index; it is useful to be able to slice in multiple dimensions as well. eg, given a 4x4 array, the top left area would be defined by the slice "[:2,:2]"
>>> a
array([[ 1, 2, 3, 4],
[ 5, 6, 7, 8],
[ 9, 10, 11, 12],
[13, 14, 15, 16]])
>>> a[:2,:2] # top left
array([[1, 2],
[5, 6]])
Ellipsis is used here to indicate a placeholder for the rest of the array dimensions not specified. Think of it as indicating the full slice [:] for dimensions not specified, so
for a 3d array, a[...,0] is the same as a[:,:,0] and for 4d, a[:,:,:,0].
Note that the actual Ellipsis literal (...) is not usable outside the slice syntax in python2, though there is a builtin Ellipsis object. This is what is meant by "The conversion of an ellipsis slice item is the built-in Ellipsis object." ie. "a[...]" is effectively sugar for "a[Ellipsis]". In python3, ... denotes Ellipsis anywhere, so you can write:
>>> ...
Ellipsis
If you're not using numpy, you can pretty much ignore all mention of Ellipsis. None of the builtin types use it, so really all you have to care about is that lists get passed a single slice object, that contains "start","stop" and "step" members. ie:
l[start:stop:step] # proper_slice syntax from the docs you quote.
is equivalent to calling:
l.__getitem__(slice(start, stop, step))
Answer from Brian on Stack OverflowEllipsis is used mainly by the numeric python extension, which adds a multidimensional array type. Since there are more than one dimensions, slicing becomes more complex than just a start and stop index; it is useful to be able to slice in multiple dimensions as well. eg, given a 4x4 array, the top left area would be defined by the slice "[:2,:2]"
>>> a
array([[ 1, 2, 3, 4],
[ 5, 6, 7, 8],
[ 9, 10, 11, 12],
[13, 14, 15, 16]])
>>> a[:2,:2] # top left
array([[1, 2],
[5, 6]])
Ellipsis is used here to indicate a placeholder for the rest of the array dimensions not specified. Think of it as indicating the full slice [:] for dimensions not specified, so
for a 3d array, a[...,0] is the same as a[:,:,0] and for 4d, a[:,:,:,0].
Note that the actual Ellipsis literal (...) is not usable outside the slice syntax in python2, though there is a builtin Ellipsis object. This is what is meant by "The conversion of an ellipsis slice item is the built-in Ellipsis object." ie. "a[...]" is effectively sugar for "a[Ellipsis]". In python3, ... denotes Ellipsis anywhere, so you can write:
>>> ...
Ellipsis
If you're not using numpy, you can pretty much ignore all mention of Ellipsis. None of the builtin types use it, so really all you have to care about is that lists get passed a single slice object, that contains "start","stop" and "step" members. ie:
l[start:stop:step] # proper_slice syntax from the docs you quote.
is equivalent to calling:
l.__getitem__(slice(start, stop, step))
Defining simple test class that just prints what is being passed:
>>> class TestGetitem(object):
... def __getitem__(self, item):
... print type(item), item
...
>>> t = TestGetitem()
Expression example:
>>> t[1]
<type 'int'> 1
>>> t[3-2]
<type 'int'> 1
>>> t['test']
<type 'str'> test
>>> t[t]
<class '__main__.TestGetitem'> <__main__.TestGetitem object at 0xb7e9bc4c>
Slice example:
>>> t[1:2]
<type 'slice'> slice(1, 2, None)
>>> t[1:'this':t]
<type 'slice'> slice(1, 'this', <__main__.TestGetitem object at 0xb7e9bc4c>)
Ellipsis example:
>>> t[...]
<type 'ellipsis'> Ellipsis
Tuple with ellipsis and slice:
>>> t[...,1:]
<type 'tuple'> (Ellipsis, slice(1, None, None))