3 ms·
Lua and Python's "not quite" ternary operator
- mikeklaas 15y agoPython has had a "quite" ternary operator for years now: expr if cond else expr
- artsrc 15y agoBut 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
- rbonvall 15y agoThat would be way odder. In Python, colons are used for statements, not expressions. Nothing wrong with the conditional expression as it is.
- lvh 15y agoHow 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.