Seek support for new slice syntax PEP.

Terry Reedy tjreedy at udel.edu
Mon Dec 14 14:18:49 EST 2009


On 12/14/2009 1:03 PM, Dave wrote:
> Just as sets may now be written as {3,'hi'}, I propose that slices
> should be available using [start:end] syntax.

I believe this has been proposed and rejected on one of the py-dev, 
py-ideas, or py-3k lists, but I would have to check to be sure.

Extended slices would also have to be allowed.

 > The Numeric community would also like this,

Evidence? Are you one of the leaders thereof?

> as would the general python user.

A few might but most would find it useless since they never write 
explicit slice objects and would have to learning something new to read 
code like the below.

Many more people uses range objects (xrange in 2.x). A range object has 
the same info as a slice object *plus* it is iterable. So it would be 
MUCH more useful if that notation created a range object.

for i in [1:n]: ...

So I would oppose the slice proposal in favor of a range proposal. 
However, his has also, I believe, been rejected, as an abbreviation too far.

> Several times now I've wanted python slice notation.  Perhaps I'll
> write a Python Enhancement Proposal.

That could be useful, even if it gets rejected. Or perhaps this should 
be added to 3099.

> edge = 4
> indexes = []
> n = edge
> nn = n**2
> for i in range(edge):
>      indexes.extend([
>          slice(i*n,(i+1)*n,1),       # rows
>          slice(i,nn,n),              # cols
>          ])
>
> row_slices = indexes[0::2]
> col_slices = indexes[1::2]
> slash = slice(n-1,n*(n-1)+1,n-1)
> backslash = slice(0,nn,n+1)
>
> Which could have been written in a manner completely consistent with
> other python shorthand notations

Python avoids getting to chicken-scratchy. There was even a proposal 
(rejected, see 3099) to deprecate [1,2,3], etc, in favor of list(1,2,3), 
etc.

 > and for which python "cannot possibly" use the notation for some 
other purpose,

But it could, see above.

> edge = 4
> indexes = []
> n = edge
> nn = n**2
> for i in range(edge):
>      indexes.extend([
>          [i*n: (i+1)*n]                  # rows
>          [i: nn: n],                      # cols
>          ])
>
> row_slices = indexes[0::2]
> col_slices = indexes[1::2]
> slash = [n-1: n*(n-1)+1: n-1]
> backslash = [0: nn: n+1]

I find this currently to be less readable.

Terry Jan Reedy




More information about the Python-list mailing list