number ranges (was Re: Matlab page on scipy wiki)

Antoon Pardon apardon at forel.vub.ac.be
Wed Feb 22 03:56:39 EST 2006


Op 2006-02-20, Steven D'Aprano schreef <steve at REMOVEMEcyber.com.au>:
> John Zenger wrote:
>
>> I strongly agree that Python should promote range or xrange to syntax. I 
>> favor [0..10] rather than [0:10] because 0..10 is inherently easier to 
>> understand.
>
> "Inherently"?
>
> You mean people are born with an instinctive, unlearnt 
> understanding of ..? Or that our brains are constructed 
> in such a way that .. is easier to understand?
>
> For what it is worth, even after years of Python 
> programming, I still sometimes write this:
>
> for i in len(myList):
>      # Oops.
>
> I too prefer range() or xrange() over magic syntax, but 
> I'm not especially a lover of the range() idiom. How 
> about this? With the introduction of a single keyword, 
> we could do this:
>
> for i in 2 to 5:
>      print i,
> which would print 2 3 4 5
>
> (I'm open to arguments that it should be more Pythonic 
> and less mathematical, and halt at 4.)
>

My suggestion would be to use what we already have and
extend that.

Python has slices. We could extend slices in two steps.

a) Let slices be iterable, so we could have something
   like:

   for i in slice(2,5):
     print i,
     
which would print 2 3 4,

Now I agree this wouldn't be a big advantage over the
range and xrange function, so the next point

b) Allow slice notation in arbitrary places. So we
   could write the above as:

   for i in (2:5):
     print i,

> A second keyword "downto" would allow easy backwards 
> loops, and a third "step" will absolutely kill any 
> chance of Guido agreeing to this whatsoever.

Well with iterable slices we wouldn't need any new
keyword nor any real new syntax. Just lift the 
limitation that slice notation is only allowed when
indexing.

-- 
Antoon Pardon



More information about the Python-list mailing list