[Python-Dev] An ability to specify start and length of slices

Tim Peters tim.one at comcast.net
Fri Jun 4 15:10:25 EDT 2004

[Noam Raphael]
> Many times I find myself asking for a slice of a specific length, rather
> than a slice with a specific end. I suggest to add the syntax
> object[start:>length] (or object[start:>length:jump]), beside the
> existing syntax.

It would be convenient at times, and I've got nothing against that.  It
isn't *necessary*, and if it were to be added the syntax should change.

Python's sequence slicing is so similar to that in the earlier Icon language
that for years I thought Guido borrowed it from Icon (but years later he
told me that's not so).  Icon has 1-based indexing (Python's is 0-based),
but apart from that they're the same.

In Icon, the notations are, e.g.,

    S[5:8]  # S[5], S[6], and S[7]
    S[5+:3] # also S[5], S[6], and S[7]

"+:" suggests addition in a way ":>" does not, so I think it's a better
notation.  Putting the '+' before the colon is also reminiscent of, e.g.

    i += j

conceptually expanding to

    i = i+j


    i +: j

conceptually expanding to

    i : i+j

is the same syntactic pattern.  I should note that Icon had the good sense
to stop there too <wink>.

More information about the Python-Dev mailing list