anything new on the ternary operator?

Ben Finney bignose-hates-spam at and-zip-does-too.com.au
Sun Jul 6 22:35:00 EDT 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
explains:

    "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."
    <http://www.python.org/peps/pep-0308.html>

FAQ 4.16 is here:
    <http://www.python.org/cgi-bin/faqw.py?req=show&file=faq04.016.htp>

-- 
 \     "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__)                                                                  |
http://bignose.squidly.org/ 9CFE12B0 791A4267 887F520C B7AC2E51 BD41714B




More information about the Python-list mailing list