bool?a:b in Python
1. Use and-or in old versions
bool and a or b
Note that a must be "true", i.e.,
for numbers: 0 - "false", others - "true"
for strings: "" - "false", others - "true"
for lists: [] - "false", others - "true"
for tuples: (,) - "false", others - "true"
for dictionaries: {} - "false", others - "true"
otherwise, one may confront weired behaviors.
For example,
(k>1) and row[k] or "."
would come to row[k] if k is greater than one, a dot otherwise.
But be careful, if row[k] is an empty string, the result will be ".", because (k>1) and False comes out False, always!
2. Improved and-or
Dive Into Python mentioned an improved and-or expression
(bool and [a] or [b])[0]
Since [a] is "true" whatever a is (even [[]] is "true"?), the and-or trick always works as expected.
3. Boolean index
Consider this
[a, b][int(not bool)]
if bool == True, not bool becomes False, int(False) returns 0, which indexes a. I have never tested this...
4. Higher versions provide a if bool else b
a if bool else b
It's the well-known story now. Although the sequence of a, bool and b seems a little strange ^_^