2 ms·
Why didn't Python do this: x = if condition a else b ?
by oxryly1 10y ago
Why didn't Python do this:
x = if condition a else b
?
- debacle 10y agoBecause what they actually want is something like: checkSomeValue() && doOtherThing()
- secstate 10y agoBecause that scans as: Variable x should be assigned if condition a true, otherwise b.
- oxryly1 10y agoBut "condition" is just a placeholder. A concrete example would be: msg = if (status == 0) "done" else "error"
- secstate 10y agoTake away the parens (which python is pretty explicit such structure should not be required to be legible). msg = if status == 0 "done" else "error" From a compiler perspective that's a bear to parse.
- scoppini 10y agoI think it could be ambiguous in certain situations. Let's say I wanted to conditionally set a variable to one of two negative constants. In the current syntax, that would look like: x = -1 if y else -2 However, with your syntax, it would look like: x = if y -1 else -2 Which could be possibly confused with trying to subtract 1 from y.
- oxryly1 10y agoAh, and Python uses whitespace and colons to disambiguate, which would ruin the efficiency of this form.
- liveoneggs 10y agotoo much like perl?