PEP308 - preference for 'x if c else y' over 'c then x else y'

Terry Reedy tjreedy at udel.edu
Sat Feb 15 11:05:20 EST 2003


"Stephen Horne" <intentionally at blank.co.uk> wrote in message
news:udpr4vk5073nmetephn2n65i5rv5fre7ou at 4ax.com...
> From the meaning/readability perspective, I think my point works
> fairly well...
>
> >>   x if c1 else y if c2 else z
>
> The end result is always the one immediately left of the first
> condition to pass (assuming an implicit always-passes condition
after
> the final else value).

While I was one of the people initially objecting to the order
reversal, it *is* a common practice in mathematical writing to put
conditions on the right of the values they select.  One can commonly
see things like
  abs(x) =  x    : x >= 0
                -x    : x <= 0
So
     return (x if c1 else
                y if c2 else
                z)
really will be readable to many people, especially when reminded of
the math language precedent.

Since this does not require a new keyword, I think it should be put
back on the table.   Unlike other proposals, we know Guido should
presumably accept it, since it was his initial suggestion
(preference?).  I now think I might prefer it to 'no change' and would
like to be able to vote it 'acceptible'.

Terry J. Reedy






More information about the Python-list mailing list