while (a=b()) ...

Paul Boddie paulb at infercor.no
Fri May 14 07:08:09 EDT 1999


Greg Ewing wrote:
> 
> "Evan Simpson" <evan at tokenexchange.com> writes:
> | while 1:
> |   value = getValue()
> | and while value:
> |   process(value)

[...]

>   while:
>     line = f.readline()
>   gives line <> "":
>     frobulate(line)
> 
> Requires a new keyword... maybe in Python 2?

Of all the suggestions this is possibly the best one I have seen,
although I am still sitting on the fence in this debate. ;-) If anything
new, in terms of changes or additions to the language syntax, must come
from this issue then I would suggest that it fit these criteria:

1. It doesn't make scanning and reading of code harder. Thus I am
somewhat
   against any proposal for something like this:

   do:
     <do something>
   do <cond1>:
     <do something>
   do <cond2>:
     <do something>

   With long loops, and plenty of blank lines, it may be difficult to
identify
   single loops at a glance.

2. It doesn't permit "cool" tricks which make code unnecessarily hard to
   follow. A classic example of this would be (from C):

   while (*a++ = *b++);

   Once understood this might be seen as being "clever", but I am of the
   opinion that code is there to be maintained in a convenient way
rather than
   be some kind of altar to a self-proclaimed "coding god".

3. It allows constructs which are intuitive, so that one is not
constantly
   having to remember that something works one way when one's intuition
tells
   you that it might work in another way. The first quoted example seems
to
   attempt to provide a split-up loop condition with a nod towards even
more
   general cases, as if we were doing something like this (in syntax
which
   finally finds a use for curly braces):

   while 1 {value = getValue()} and value:
     process(value)

   # Exploit this further.

   while 1 {value = getValue()} and value {otter = getAnimal(value)} and
otter:
     process(otter)

   The second quoted example seems more specialised, but could appeal to
one's
   intuition more. In saying that "we are getting values from this
source on
   every iteration and will continue given this condition being true" it
   conveys a simpler message, although the keywords might still need to
be
   better chosen:

   using:                  # or with, having, <insert favourite name
here>
     line = f.readline()
   while line:
     do_the_business(line)

   The first example I gave (point 1) doesn't appeal to me so much (but
then I
   only mentioned it so that I could criticise it). One could interpret
this as
   being a sequence of loops where the flow of execution starts at the
top and
   iteration is confined to the top loop until <cond1> is met, at which
point
   the next loop is entered and iteration is confined only to that block
until
   <cond2> is met. There certainly is room for ambiguity in the
interpretation
   of this kind of construct if one has only a passing familiarity with
it.

Anyway, I feel that this discussion is not necessarily pointless because
people don't have a need for improvements in this area of Python, but
rather because people easily divide into the two main camps: those who
just want to do "while a = b" because they are comfortable with it (from
C-style languages), and those who don't find that acceptable at all.

Paul




More information about the Python-list mailing list