Numpy, for example, allows multi-dimensional slices:
a[:, 0, 7:9]
This raises the question: what else is possible? (Imagine the possibilities!)
According to this answer and some experimentation (see below), if there is a comma, Python builds a tuple of objects, some of which may be slice objects, and passes it (as key) to __getitem__(self, key) of a.
The documentation for __getitem__(..) doesn't specify this behaviour. Is there any official documentation that I missed? In particular, how backwards-compatible is this syntax? (Searching the web for "python extended slice notation" gives "What's new in Python 2.3", which doesn't mention it.)
Experimentation
>>> class Test(object):
... def __getitem__(self, x):
... print repr(x)
>>> t = Test()
First, things that Python finds recognisable for multi-slicing:
>>> t[1]
1
>>> t['a':,]
(slice('a', None, None),)
>>> t['a':7:('b','c'),]
(slice('a', 7, ('b', 'c')),)
# Seems like it can be arbitrary objects?
>>> t[(t,t):[4,5]]
slice((<__main__.Test object at 0x07D04950>, <__main__.Test object at 0x07D04950>), [4, 5], None)
>>> t[::]
slice(None, None, None)
>>> t[:]
slice(None, None, None)
>>> t[::,1,::,::,:,:,:]
(slice(None, None, None), 1, slice(None, None, None), slice(None, None, None), slice(None, None, None), slice(None, None, None), slice(None, None, None))
>>> t[...]
Ellipsis
>>> t[... , ...]
(Ellipsis, Ellipsis)
>>> t[ . . . ]
Ellipsis
Some things that are NOT allowed (SyntaxError):
# Semicolon delimiter
t['a':5; 'b':7:-7]
# Slice within a slice
t['a':7:(9:5),]
# Two trailing commas
t[5,,]
# Isolated comma
t[,]
# Leading comma
t[,5]
# Empty string
t[]
# Triple colon
t[:::]
# Ellipses as part of a slice
t[1:...]
t[1:2:...]
# Ellipses inside no-op parens:
t[(...)]
# Any non-zero and non-three number of dots:
t[.]
t[..]
t[ . . . . ]
t['a':7:('b','c'),]to work? That's a bug, I tested Python 2.4 and up and they all reject the lower bound and stride by raising aTypeErrorexception. Same fort[(t,t):[4,5]]. I no longer have a Python 2.3 build.2.7.12 (v2.7.12:d33e0cf91556, Jun 27 2016, 15:19:22) [MSC v.1500 32 bit (Intel), I think that's a standard install from the official Python website. (Also, just checked that it also behaves that way when used directly in the terminal — it does.)