[Python-ideas] Possible new slice behaviour? Was ( Negative slice discussion.)
Ron Adam
ron3200 at gmail.com
Tue Nov 5 19:13:50 CET 2013
On 11/05/2013 05:02 AM, Nick Coghlan wrote:
> > Are there plans for pythons builtin types to use multidimensional indexing?
>
> Yes, memoryview already has some support for multidimensional array shapes,
> and that's likely to be enhanced further in 3.5.
Ok, I suppose other builtin types may (or may not) follow that pattern.
But in this light, I agree, it's best not to create a more complex pattern
to handle for those cases.
(... and it worked so nicely, Oh Well.)
The alternative is to have a function that does what the slice syntax does.
And then extend that. It seems to me it's a good idea to have function
equivalents of syntax when possible in any case.
do_slice(obj, slices, *callables)
Where slices is either a single slice or a tuple of slices or indices.
# Examples
class GetSlice:
"""Return a slices from slice syntax."""
def __getitem__(self, slc):
return slc
gs = GetSlice()
seq = list(range(10))
print(do_slice(seq, gs[1, 5, 7]))
print(do_slice(seq, gs[3:7], openi))
print(do_slice(seq, gs[3:7], closedi))
print(do_slice(seq, gs[3:7], closedi, onei))
print(do_slice(seq, gs[3:5, 7:8, 9], reversei))
print(do_slice(seq, gs[:], reversei))
print(do_slice(seq, range(-5, 15), wrapi))
print(do_slice(seq, range(15, -5, -1), wrapi))
"""
[1, 5, 7]
[4, 5, 6]
[3, 4, 5, 6, 7]
[2, 3, 4, 5, 6]
[[4, 3], [7], 9]
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
[5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4]
[5, 4, 3, 2, 1, 0, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 9, 8, 7, 6]
"""
Cheers,
Ron
More information about the Python-ideas
mailing list