For review: PEP 308 - If-then-else expression
Erik Max Francis
max at alcyone.com
Sat Feb 8 06:03:32 CET 2003
Peter Hansen wrote:
> I hadn't thought of C, but I wonder if you're really correct. I
> to make such things fairly readable, even when complex, with
> a = (expr1)
> ? (cond1)
> : (expr2)
> ? (cond2)
> : (expr3)
> ? and so on...
> (It looks more readable in real code than it does above, although
> get me wrong: I'm not saying it's _really_ readable, just not bad.)
This is obviously a judgement call, so we can agree to disagree.
Chained conditional expressions (in whatever language) aren't the end of
the world, but in my opinion they're a case of taking a shortened syntax
The benefit of a conditional expression (again, in whatever language)
is, in my opinion, the ability to do a very simple if...else switch as
an expression (not a statement) in a place where the emphasis is _not_
on that switch and in a manner that is fairly readable.
So you'd want conditional expressions in things like [example in C]:
printf("I have %d %s%s.\n", count, noun, count == 1 ? "" : "s");
Here's an example where I want to do an if...else but that is really a
minor point the big picture. The if...else is necessary, but it isn't
the main focus. When you're doing a lot of output, you'll often find
this kind of thing going on a great deal. I certainly find it happening
a lot in Python, and rather than having to 1. have a big block of simple
if ...: else: ... statements setting simple variables which I use
immediately after or 2. use one of the conditional expression wannabe
expressions, each of which has readability/true-to-form tradeoffs, I'd
much rather just have a proper conditional expression built in to the
If you end up with an expression that consists of a huge chain of
conditional expressions (your example was simply an assignment), that
begs the question of what the crux of the purpose of that statement;
obviously, the repeated if...else if...else is what's paramount, and so
using the shortened syntax seems to me a priori less readable.
Indentation in C can make that more readable, but it can in Python too,
after all (this is an expression we're talking about here there are no
a = c1 if e1 else \
c2 if e2 else ...
or surrounding it in parentheses or putting the indentation elsewhere or
whatever variation you prefer. (We can get into discussions about which
formof indentation will look better for Python's conditional operator,
but that's not my crucial point here; my point is you can indent Python
conditional expressions for readability just as well as you can indent C
I just find that in the case where I find chained conditional expression
in C or C++, it's usually more obfuscated than it's worth; for a simple
assignment, certainly, one immediately wonders why you didn't just use a
repeated if...else if...else.
> Just curious whether the strange inversion
> of expr/condt/condf to condt/expr/condf actually works against this
> type of "readability assist".
This really isn't of much concern to me, since right away if you're
chaining conditional expressions you're winding down the readability
index of your code. All that's important is that the expressions chain
properly -- that is,
a if b else c if d else e
a if b else (c if d else e)
(as it does in the current proposal); beyond that, if you _really_ want
to use the construct in order to obfuscate rather than simplify, that's
every programmer's prerogative.
Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
__ San Jose, CA, USA / 37 20 N 121 53 W / &tSftDotIotE
/ \ The doors of Heaven and Hell are adjacent and identical.
\__/ Nikos Kazantzakis
Bosskey.net: Return to Wolfenstein / http://www.bosskey.net/rtcw/
A personal guide to Return to Castle Wolfenstein.
More information about the Python-list