For review: PEP 308 - If-then-else expression
dave at pythonapocrypha.com
Sat Feb 8 20:06:52 CET 2003
On Sat, 8 Feb 2003, holger krekel wrote:
> Andrew Koenig wrote:
> > Laura> Thus answering the question 'if we let it in will we get code that
> > Laura> is hard to read'. I want my control flow with indentation, please.
> > holger> very good point.
> > Yes, but in what direction does it argue?
> good point :-)
> > In C, I don't consider ?: to be control flow, any more than
> > I consider && or || to be control flow. In fact, part of
> > reason that conditional expressions appeal to me is that they
> > permit some kinds of expressions to be written *without* control
> > flow that otherwise would require it.
> IMO Laura emphasized how good enforcing indentation is for
> readability. Writing 'x and y or z' or 'x if y else z'
> (or list-comps for that matteR) undermines readability.
Are you sure? Some aspects of readability are completely subjective - lots
of people hate list comps but I (and others) always find them to be
really, really readable and they just "make sense".
> Beeing able to concisely express a "binary choice"
> in an expression of course can be nice in some cases.
> It's a tradeoff between readability and 'expression power' but i think
> that readability should score higher here.
I agree with you, and that's why I'm +1 on the PEP :) - when used properly
the if-expr IMO *does* increase readability - it very clearly announces
that a binary choice is being made (and that possibly for short-circuiting
reasons) while a standard if statement is ambiguous - is a choice being
made or is it for higher-level control flow?
I guess I'm just emphasizing that, for or against the PEP, we should all
be careful about including "increased readability" in our list of reasons
- if you had been using if-expressions all along you would probably
consider them to be quite readable. Now, exuse me while I go argue some
more that if-exprs can make code more readable. ;-)
More information about the Python-list