Is this PEP-able? fwhile

Chris Angelico rosuav at gmail.com
Wed Jun 26 03:40:55 EDT 2013


On Wed, Jun 26, 2013 at 10:47 AM, Dennis Lee Bieber
<wlfraed at ix.netcom.com> wrote:
> On Tue, 25 Jun 2013 17:20:43 +1000, Neil Hodgson <nhodgson at iinet.net.au>
> declaimed the following:
>
>>jimjhb at aol.com:
>>
>>> Syntax:
>>> fwhile X in ListY and conditionZ:
>>
>>    There is precedent in Algol 68:
>>
>>for i from 0 to n while safe(i) do .. od
>>
>         The REXX variant would be
>
> do for i = 0 to n while safe(i)
>         ...
> end
>
>         Basically one has an optional "for" clause ( for index = initial to end
> by step ), and one has an optional while/until clause -- Hmm, wonder if
> some interpreters would parse both while and until <G>. I need to install
> Regina Rexx on this new machine...

Modulo the 'for' keyword, which is superfluous there. Here's a test
script I knocked up on my OS/2 box back home:

/* */
do i=0 to 9 while safe(i)
        say i" is safe"
end
exit

safe: procedure
return arg(1)\=6

The \= in the last line is the REXX "not-equal" operator, like != in
Python. This outputs:

0 is safe
1 is safe
2 is safe
3 is safe
4 is safe
5 is safe

and then terminates. It's pretty clean; the DO/END construct defines a
block, and may optionally execute it more than once. With no
arguments, it just creates a block that executes once (equivalent to
C's braces); valid args include FOREVER (infinitely loop), WHILE
condition (iterate while condition is true), UNTIL condition (execute
once, then check condition, iterate while condition is false - like a
do/while in C), var=value (eg "I=1" - set var to value, then increment
by 1 or by the "BY" value, continue forever or until the "TO" value),
and possibly another that's slipped my mind. Aside from FOREVER, which
stands alone, they're completely independent.

But that's syntax, lots of it. What I'd like to see in Python is
simply a bit of flexibility in the rule about newlines. The for/if
construct in Python could be exactly the same as it now is, only with
the two statements on one line, and it would look very similar to the
existing notation. I can already one-line a simple statement:

for i in range(10): print(i)

I just can't put in an if:

>>> for i in range(10): if i%3: print(i)
SyntaxError: invalid syntax

But I can, as long as I use expression-if:

>>> for i in range(10): print(i) if i%3 else None

Seriously, I can use Perl-style notation to achieve this. Does that
seem right to you? *boggle*

ChrisA



More information about the Python-list mailing list