Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
Lua and Python's "not quite" ternary operator (hisham.hm)
6 points by hisham_hm on May 5, 2011 | hide | past | favorite | 4 comments


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


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.


That would be way odder. In Python, colons are used for statements, not expressions. Nothing wrong with the conditional expression as it is.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: