Re: range() (fwd)

Guido van Rossum (Guido.van.Rossum@cwi.nl)
Tue, 30 Jun 1992 10:50:17 +0200

Lou Kates writes:

>Its a judgement call as to whether range() should behave nicely
>under proportionality or reversal since it appears that it won't
>behave nicely under both.
>
>I have to admit that I prefer the proportionality identity which
>means that range() would stay as it is.

Well, the law of the least hassle also suggests that it should stay
that way, unless someone else will start a major upraising...

>Getting back to the original example, I guess the moral of the
>story is that if one wants to elegantly express backwards
>iteration over list indices one should use the idiom:
>
> for i in range(len(a)).reverse():

Unfortunately, list.reverse() is a procedure (== function returning
None) that has a side effect (reversing the list in place) and does
not return a value, so in real current Python you have to write:

tmp = range(len(a))
tmp.reverse()
for i in tmp: ...

In general, Python procedures can't be changed to functions without
breaking existing code since the interpreter prints the value of an
expression-statement if it isn't otherwise used. If this were deemed
useful enough it would be easy to add a method list.reversed() that
would return a reversed copy. For consistency we would then also need
a list.sorted() method.

>P.S. Note how the above discussion required APL-like vector
> multiplication to state the proportionality property even in
> this apparently not heavily mathematical setting!

"Required" seems a bit strong here, especially from someone who has
already admitted he's got an APL mindset. :-)

--Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
"It happens sometimes -- people just explode. Natural causes."