[Python-Dev] Lukewarm about range literals

Tim Peters tim_one@email.msn.com
Mon, 28 Aug 2000 21:51:33 -0400


Just brain-dumping here:

Thomas did an excellent job on the patch!  It's clean & crisp and, I think,
bulletproof.  Just want that to be clear.

As the reviewer, I spent about 2 hours playing with it, trying it out in my
code.  And I simply liked it less the more I used it; e.g.,

for i in [:len(a)]:
    a[i] += 1

struck me as clumsier and uglier than

for i in range(len(a)):
    a[i] += 1

at once-- which I expected due to the novelty --but didn't grow on me at
*all*.  Which is saying something, since I'm the world's longest-standing
fan of "for i indexing a" <wink>; i.e., I'm *no* fan of the range(len(...))
business, and this seems even worse.  Despite that I should know 100x better
at all levels, I kept finding myself trying to write stuff like

for i in [:a]:  # or [len(a)] a couple times, even [a:] once
    a[i] += 1

Charles likes slices.  Me too!  I *love* them.  But as a standalone notation
(i.e., not as a subscript), part of the glory of slicing breaks down:  for
the list a, a[:] makes good sense, but when *iterating* over a,  it's
suddenly [:len(a)] because there's no context to supply a correct upper
bound.

For 2.0, the question is solely yes-or-no on this specific notation.  If it
goes in, it will never go away.  I was +0 at first, at best -0 now.  It does
nothing for me I can't do just as easily-- and I think more clearly --with
range.  The kinds of "extensions"/variations mentioned in the PEP make me
shiver, too.

Post 2.0, who knows.  I'm not convinced Python actually needs another
arithmetic-progression *list* notation.  If it does, I've always been fond
of Haskell's range literals (but note that they include the endpoint):

Prelude> [1..10]
[1,2,3,4,5,6,7,8,9,10]
Prelude> [1, 3 .. 10]
[1,3,5,7,9]
Prelude> [10, 9 .. 1]
[10,9,8,7,6,5,4,3,2,1]
Prelude> [10, 7 .. -5]
[10,7,4,1,-2,-5]
Prelude>

Of course Haskell is profoundly lazy too, so "infinite" literals are just as
normal there:

Prelude> take 5 [1, 100 ..]
[1,100,199,298,397]
Prelude> take 5 [3, 2 ..]
[3,2,1,0,-1]

It's often easier to just list the first two terms than to figure out the
*last* term and name the stride.  I like notations that let me chuckle "hey,
you're the computer, *you* figure out the silly details" <wink>.