Conditional Expressions don't solve the problem

Terry Reedy tjreedy at home.com
Wed Oct 17 13:13:59 EDT 2001


"Christopher A. Craig" <com-nospam at ccraig.org> wrote in message
news:mailman.1003321457.28474.python-list at python.org...
> Paul Rubin <phr-n2001d at nightsong.com> writes:
>
> > Having to split it into several statements clutters up the code
and
> > makes the readability WORSE by moving the test condition away from
the
> > 'while' keyword.  Do you REALLY find
> >
> >     while (line := file.readline()) != 'end':
> >         process_line(line)
> >
> > to be less readable than
> >
> >     while 1:
> >        line = file.readline
> >        if line == 'end': break
> >        process_line (line)
>
> No, but I find both much less readable than
>
> # Python 2.2+
> for line in file:
>   if line=='end': break
>   process_line(line)

One can even wrap such proprietary, special-purpose end-of-sequence
conditions in an iterable class instance.  The following seems to me
to be conceptually even cleaner and even easier to read:

for line in filechunk(file, 'end'):
  process(line)

Terry J. Reedy






More information about the Python-list mailing list