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

Tim Peters tim.one at comcast.net
Sun Jun 6 01:17:32 EDT 2004


[Noam Raphael]
> Ok, why not? Long live +: ! So what do people think of the improved,
> rational syntax? Perhaps someone who have used Icon can share his/her
> experience with us?

My recollection was that it was handy more than just a few times, but
reviewing some Icon code reminded me of why that was so (see below).

In addition to +:, Icon slices also support -:.  This relates to another
difference in Icon slicing:  S[i:j] means the same thing as S[j:i] in Icon.
In Python, for fixed S, i and j, at least one of those forms is an empty
slice.

In the Icon program library distributed with Icon 9.4, there are about 200K
lines of Icon source in about 1200 files.  139 lines match the regexp

    [-+]\s*:(?!=)

(since Icon uses := for assignment, the negative lookahead on "=" is needed
to weed out the Icon augmented assignments +:=, -:=, ++:= and --:=)

So it's not used a lot by Icon experts (< 1 per 1000 lines).  What staring
at this reminded me of (and which I had forgotten) is that the most *common*
use of +: in Icon is slices of the form

    S[1+:n]

to get the leftmost n elements.  Python spells this

    S[:n]

but specifying a starting index isn't optional in Icon.  The handful of uses
of -: outside the test suite are all of the form

    S[0:-n]

which Python spells

    S[-n:]

(get the rightmost n elements).

Several dozen "non-trivial" uses of +: remain, but it's hard to get excited
about them; e.g.,

    line[x+:2] := "["

is marginally easier than

    line[x:x+2] := "["

but Python doesn't support assigning to string slices anyway.

    "abcde"[2+:2]

was an elaborate way to spell

    "bc"

This is a good use:

   every push(layout,show(deck[(0 to 3) * handsize + 1 +: handsize]))

The closest Python is

   layout = [show(deck[i*handsize:(i+1)*handsize]) for i in range(4)]

and

   layout = [show(deck[i*handsize +: handsize]) for i in range(4)]

would indeed be clearer.

That's not enough (IMO) to be worth the pain of changing the language,
though.  Note that if you have a lot of fixed-field strings to parse, the
struct module already supplies a nice way to spell that; e.g., to pick apart
8-character HH:MM:SS strings,

>>> def hms(s):
...     import struct
...     return struct.unpack('2sx2sx2s', s)
...
>>> hms('01:12:45')
('01', '12', '45')
>>>

That's better (clearer, easier, faster) than coding a bunch of fiddly
indexing too.





More information about the Python-Dev mailing list