Starting with Python 2.5, you can:
value = b if a > 10 else c
Previously, you had to do something like the following, although the semantics are not identical, because the effect of a short circuit is lost:
value = [c, b][a > 10]
There is also another hack using "and ... or", but it is better not to use it, because it has undesirable behavior in some situations, which can lead to difficult detection of an error. I wonβt even write a hack because I think itβs better not to use it, but you can read about it on Wikipedia if you want.
Mark Byers Feb 03 '10 at 12:44
source share