4 comments

[ 3.0 ms ] story [ 19.6 ms ] thread
Python has had a "quite" ternary operator for years now:

  expr if cond else expr
But this odd order is a hack for the fact that python distinguishes between expressions and statements and won't change. It should be:

    result = if cond: expr else: expr
That would be way odder. In Python, colons are used for statements, not expressions. Nothing wrong with the conditional expression as it is.
How is the order important? Why couldn't Python have implemented:

    result = cond then expr1 else expr2
(Except that it's harder to read, which is why Python doesn't have that.)

IMNHSO, precisely for the reason you mention, that would be much more of a hack.