3 ms·
The danger here is that this will fall through to the falseaction if trueaction returns a false result ("", False, 0, None, [], {}, etc). For example, this wil
by myelin 18y ago
The danger here is that this will fall through to the falseaction if trueaction returns a false result ("", False, 0, None, [], {}, etc). For example, this will evaluate to "etc":
True and "" or "etc"
So people started doing this instead, as [False] is still 'true' (non-empty lists are 'true' in Python):
(condition and [trueaction] or [falseaction])[0]
... which is a bit crufty. So this was added:
trueaction if condition else falseaction
- thomasmallen 18y agoAh, that is much better. I've been learning from Dive Into Python which I know is a tad dated, and Pilgrim (sort of) endorses the and...or method.