PEP 308: A PEP Writer's Experience - PRO

Paul Rubin phr-n2003b at NOSPAMnightsong.com
Sat Feb 8 16:29:39 EST 2003


Michael Chermside <mcherm at destiny.com> writes:
> There are lots of arguments in favor of a ternary expression,
> but there are just two which I feel are quite convincing.
> 
> [1] It makes expressions more powerful.
> [2] It distinguishes choice from branching

I think Andrew Koenig pointed out a third argument which many of us
had perhaps underestimated: it can prevent bugs.  A lot of the
workarounds attempted for its absence are flat out wrong.  He gives an
example showing finding the maximum of x and y with

   x > y and x or y

which looks reasonable and might even pass the initial unit tests of a
real program, but gives the wrong answer (zero) if x is 0 and y is
negative.  A conditional expression like

   x if (x > y) else y

is much harder to get wrong.




More information about the Python-list mailing list