[Python-Dev] PEP Proposal: Revised slice objects & lists use slice objects as indexes
Forrest Voight
voights at gmail.com
Mon Mar 10 00:21:31 CET 2008
This would simplify the handling of list slices.
Slice objects that are produced in a list index area would be different,
and optionally the syntax for slices in list indexes would be expanded
to work everywhere. Instead of being containers for the start, end,
and step numbers, they would be generators, similar to xranges.
Lists would accept these slice objects as indexes, and would also
accept any other list or generator.
Last, slice objects would be able to be added for multiple index
ranges of a list. The new slice object would keep a list of ranges.
Optionally, the 1:2 syntax would create a slice object outside of list
index areas. It would be shorthand for slice, as [] is for list. This
would create some confusion in loops and conditionals due to the colon
being used for the end of the structure. (see last example)
This would be incompatible with classes that define __setitem__ and
__getitem__, and would need changes in how slices are handled in
CPython. Therefore, this is probably a proposal for Python 3000.
Examples:
>>> 1:5
1:5
>>> list(1:5)
[1, 2, 3, 4]
>>> list(1:5:2)
[1, 3]
>>> s = 1:3
>>> range(5)[s]
[1, 2]
>>> 1:5 + 15:17
1:5 + 15:17
>>> range(30)[1:5 + 15:17]
[1, 2, 3, 4, 15, 16]
>>> range(100)[[1,2,3]]
[1, 2, 3]
>>> range(100)[1,2,5] # maybe
[1, 2, 5]
>>> for x in :15:3: print x # maybe
0
3
6
9
12
---
Forrest Voight
More information about the Python-Dev
mailing list