What I learned:
In Dive in to Python, I read about the peculiarities of the and and or operators, and how the evaluation of short circuits of Boolean operators can be used to more accurately express conventions through and / or a trick that is very similar to the triple operator in C.
WITH
result = condition ? a : b
Python:
result = condition and a or b
This seems useful since lambda functions are limited to single-line in Python, but it uses logical syntax to express control flow.
Since Python 2.5, the built-in if seems to have come to the rescue as a more readable syntax for and / or trick:
result = a if condition else b
Therefore, I suppose this is a pythonic replacement for a less readable and-or-else construct. Even if I want to nest a few conditions, it still looks pretty comprehensive:
result = a if condition1 else b if condition2 else c
But in a world of uncertainty, I often find that I wrote such code to access abc:
result = a and hasattr(a, 'b') and hasattr(ab, 'c') and abc or None
So, using inline β if I could probably get rid of some ands and ors, which led to a quite readable piece of code:
result = abc if hasattr(a, 'b') and hasattr(ab, 'c') else None
I also discovered a somewhat hidden approach for conditonals in this recipe
result = (a, b)[condition]
but this is not short-circuited and will lead to all kinds of errors if the result of the condition does not return a boolean value of 0 or 1.
What I would like to know:
Now I wonder if it would be preferable / more pythonic to use inline- if as much as possible, if downstream compatibility is not a problem, or is it just a matter of taste and how much does it feel at home in the world of short circuit assessment?
Update
I just realized that inline-if is more than syntactic sugar for and-or-trick, as it will not fail if a is false in a boolean context. Therefore, it is probably more fault tolerant.