Range Operation pre-PEP

Duncan Booth duncan at NOSPAMrcp.co.uk
Wed May 9 06:22:53 EDT 2001


Carlos Ribeiro <cribeiro at mail.inet.com.br> wrote in
<mailman.989362093.17780.python-list at python.org>: 

>>(1 .. 5) % 3
> 
> This one is a little bit harder. Where did you take the 3 on the
> example above? I could not understand the logic of '(1 .. 5) % 3'. I
> think that the range operator should specify the step; something like
> '1 .. 5 % 2' is clearer in my opinion. The use of '%' as 'step'
> operator is highly debatable, though. Why not use any of the characters
> [!@#$&] ? <wink> 
> 

Why not add a 'step' method to iterators and sequence types?
    	step(self, step, start=0)
    	Returns a new iterator or sequence containing every 'step'
    	value from index 'start' in the original sequence.
For iterators this would return a new iterator, for sequences it would 
return a new sequence. If you want a step iterator from a sequence you just 
have to do: iter(sequence).step(n)

So:

>>> range(1, 11).step(2)
[1, 3, 5, 7, 9]
>>> (1..10).step(2)
<some iterator type returning 1, 3, 5, 7, 9 successively>
>>> range(1, 11).step(2, 1)
[2, 4, 6, 8, 10]
>>> lst = range(1, 11)
>>> zip(lst.step(2), lst.step(2, 1))
[(1, 2), (3, 4), (5, 6), (7, 8), (9, 10)]

-- 
Duncan Booth                                             duncan at rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?



More information about the Python-list mailing list