[Python-ideas] [Python-Dev] PEP Proposal: Revised slice objects & lists use slice objects as indexes

Neil Toronto ntoronto at cs.byu.edu
Mon Mar 10 07:28:43 CET 2008


Alexandre Vassalotti wrote:
> On Sun, Mar 9, 2008 at 7:21 PM, Forrest Voight <voights at gmail.com> wrote:
>> 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.
> 
> I am not sure what you are trying to propose here. The slice object
> isn't special, it's just a regular built-in type.
> 
>   >>> slice(1,4)
>   slice(1, 4, None)
>   >>> [1,2,3,4,5,6][slice(1,4)]
>   [2, 3, 4]
> 
> I don't see how introducing new syntax would simplify indexing.

Likewise. It would simplify looping, though:

     >>> for i in 1:5:
     ...     print i
     1
     2
     3
     4
     >>>

Since this kind of loop happens frequently, it makes sense to shorten 
it. Slice objects (and syntax) seem ready-made for that - it wouldn't be 
*new* syntax, just repurposed syntax.

Though Forrest didn't bring this up directly, I've often thought that 
Python's having both xrange and slice (and in 3000, range and slice) is 
mostly vestigial. Their information content is identical and their 
purposes are highly analogous. Unifying them would reduce the number of 
new concepts for a beginner by one, and these are frequently-used 
concepts at that.

Negative indexes could throw the idea for a loop, though. (Pun! Ha ha!) 
And this makes the colons look like some kind of enclosure:

     >>> for i in :5:
     ...

Neil



More information about the Python-ideas mailing list