Conditional Expressions don't solve the problem

Christopher A. Craig com-nospam at ccraig.org
Wed Oct 17 08:23:44 EDT 2001


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)

or 

# Python 2.1+
for line in file.xreadlines():
  if line=='end': break
  process_line(line)

> The .py files in the standard library aren't really the right place to
> look.  They were written without those constructs and the authors may
> have formulated particular pieces of code in ways considerably
> different than if they had conditional expressions available (as
> opposed to just splitting the expressions into a few statements).
>
> It's better to scan through the C code of the interpreter and the
> library C modules, rather than the .py files, and see how many places
> ?: and assignments in expressions are used.

But that doesn't give you a good feeling for how much conditional expressions
help readability.  As the above examples show, just because something makes
sense in C that doesn't mean it makes sense in Python.  Also if the lack of
conditional expressions causes authors to formulate code in such a way that
adding them does not increase readability, that is strong support for leaving
them out.  I agree with Guido.  If there aren't a significant number of places
where conditional expressions will benefit the readability of the standard
library, there probably isn't sufficient reason to make the change.


-- 
Christopher A. Craig <com-nospam at ccraig.org>




More information about the Python-list mailing list