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

Steven D'Aprano steve at REMOVEMEcyber.com.au
Mon Feb 20 03:15:36 EST 2006


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.)

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



> Haskell also has a good step notation.  In Haskell:
> 
> [1..10] means [1,2,3,4,5,6,7,8,9,10]
> [1,3..10] means [1,3,5,7,9]

I'm wary of that notation. It is too easy to make 
typos, what with , and . next to each other, and the 
typos often will not raise an exception but will simply 
give incorrect but puzzling behaviour. This isn't 
unique to the proposed syntax (e.g. under Python today 
it isn't obvious whether [0,3] is a typo for [0.3]) but 
it gives me pause.


-- 
Steven.




More information about the Python-list mailing list