Conditional Expressions don't solve the problem

Paul Rubin phr-n2001d at nightsong.com
Wed Oct 17 04:06:28 EDT 2001


Guido van Rossum <guido at python.org> writes:
> On the other hand, the ability to hide an assignment inside an
> expression is IMO purely a way to save some keystrokes: the variable
> needs to be named anyway, so you may as well do the assignment in a
> separate step so that the reader is alerted of it.

But it's not just saving keystrokes--the usual place for an assigment
in an expression is in the control expression for a while loop.
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)

(or worse, imagine several nested loops like that)?  I don't.

> That said, I'm still looking for decent examples of places in the
> standard library that would become more readable with a conditional
> expression...  Without more motivation, I think I'll hold back.

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.

That said, there's enough alternatives hashed out on the thread
without a clear winner emerging that it's best to not put conditional
expressions into 2.2.  




More information about the Python-list mailing list