In most cases, you want to use a conditional expression instead:
myvar = b if a else c
A short circuit is very Pythonic, but just be aware of the pitfalls, where b is false-y; using a short circuit will lead to a different result in this case. In most cases, you do not want to assign c .
Even in this case, you can get the same result with the adjusted condition:
myvar = b if a and b else c
Short circuit is great for default values:
foo = somevar or 'bar'
or to ensure compliance with the prerequisites:
foo = somevar and function_raises_exception_if_passed_empty_value(somevar)
source share