anything new on the ternary operator?

Ben Finney bignose-hates-spam at
Mon Jul 7 04:35:00 CEST 2003

On Mon, 07 Jul 2003 02:13:15 GMT, Russell Reagan wrote:
> What is superior about using the proposed ternary operator instead of
> using the 'and' and 'or' operators to simulate inline logic?

The "cond and expr1 or expr2" does not always function as expected for a
ternary if-then-else operator.  The PEP for the ternary operator

    "A common way to emulate an if-then-else expression is:

        <condition> and <expression1> or <expression2>

    However, this doesn't work the same way: it returns <expression2>
    when <expression1> is false!  See FAQ 4.16 for alternatives that
    work -- however, they are pretty ugly and require much more effort
    to understand."

FAQ 4.16 is here:

 \     "I wish I had a dollar for every time I spent a dollar, because |
  `\        then, yahoo!, I'd have all my money back."  -- Jack Handey |
_o__)                                                                  | 9CFE12B0 791A4267 887F520C B7AC2E51 BD41714B

More information about the Python-list mailing list