[Python-Dev] Adding a conditional expression in Py3.0

Thomas Lotze tl at gocept.com
Wed Sep 21 00:59:49 CEST 2005


Barry Warsaw wrote:

>> x = (if a then
>>         b
>>      elif c then
>>         d
>>      else
>>         e
>>     )
[...]
> 
> I guess that's my point.  To me, your latter is actually worse than
> 
> if a:
>     x = b
> elif c:
>     x = d
> else:
>     x = e

Can't see a difference as far as readability is concerned. But then,
tastes differ.

> I think the conditional stuff gets the most bang for the buck in
> situations like:
> 
> d[foo] = if a then b else c

And I think similar holds for LCs and GEs. Unwinding a complex sequence of
for and if clauses is certainly no fun unless one is really used to it.
(Which doesn't take long, though.)

But your example poses another question: Up until now, I had the
impression that parentheses should be mandatory around a conditional
expression. There's certainly no avoiding them in situations like
(if x then 1 else 2) + 3. But what about unambiguous cases like your
example line?

-- 
Thomas




More information about the Python-Dev mailing list