[Python-ideas] Allowing breaks in generator expressions by overloading the while keyword

Greg Ewing greg.ewing at canterbury.ac.nz
Fri Feb 21 23:58:40 CET 2014


Antoine Pitrou wrote:
> But on the contrary, trying to compress control flow in a single line
> breaks the visual structure expectation for Python code (in which most
> control flow is introduced by a colon and a linefeed).

We've had control flow in expressions for a long time. The
'and' and 'or' operators affect control flow, but nobody
complains that they "break the visual flow".

Quite the opposite: they're useful because they *maintain*
the visual flow. They allow the code to be read declaratively
without thinking in terms of control flow.

The same thing applies to the if-expression, and comprehensions,
and the proposed except-expression. They're attractive because
they expand the range of code that can be written in a
declarative rather than an imperative style.

Often they allow something to be written on a single line
that would otherwise require multiple lines, but that's not
the only reason for using them. In fact, I would say that
using them *just* to save space is often a bad idea -- if
the code isn't easier to read when written declaratively,
then you shouldn't be writing it that way, even if it is
shorter.

-- 
Greg




More information about the Python-ideas mailing list