PEP 284, Integer for-loops
phil hunt
philh at comuno.freeserve.co.uk
Wed Mar 6 19:49:14 EST 2002
On Thu, 7 Mar 2002 00:28:30 +0000, phil hunt <philh at comuno.freeserve.co.uk> wrote:
>On Wed, 06 Mar 2002 09:08:57 -0800 (PST), Sean 'Shaleh' Perry <shalehperry at attbi.com> wrote:
>>I am a little confused.
>>
>>Your proposal starts with the claim that people are looking for a natural way
>>to loop over a range of integers.
>
>How about:
>
> for i = 1 to 10:
>
>or
>
> for i is 1 to 10:
>
>I think these are both equally good and both much preferable over
>the horrible
>
> for 1 <= i <= 10:
>
>which I can only assume is someone's sick idea of a joke.
>
>This syntax could easily be extended, to allow a step other than 1:
>
> for i = 1 to 10 step 3:
And extended in other ways, to make it similar to how range() and
xrange() work.
for i = 1 tobefore 10: print i,
would print 1 2 3 4 5 6 7 8 9
for i = to 5: print i,
=> 0 1 2 3 4 5
for i = tobefore 5: print i,
=> 0 1 2 3 4
for 5: print "x",
=> x x x x x
The extra syntax I am proposing is:
for_stmt : for_line ":" suite ["else" ":" site]
for_line : traditional_for_line /* as before */
| to_for_line
| repeat_for_line
to_for_line : "for" target "=" to_expression
to_expression : [expression] to_clause [step_clause]
to_clause : "to" expression | "tobefore" expression
step_clause : "step" expression
repeat_for_line : "for" expression
--
<"><"><"> Philip Hunt <philh at comuno.freeserve.co.uk> <"><"><">
"I would guess that he really believes whatever is politically
advantageous for him to believe."
-- Alison Brooks, referring to Michael
Portillo, on soc.history.what-if
More information about the Python-list
mailing list