I need to do something like this:
if A
function(a)
elif B
function(b)
else
function(c)
I found here simpler version:
function(a if A else c)
Is there version for elif
? Something like:
function(a if A b elif B else c)
How should I write this (if this exists)? The code above doesn't look right.
No, no elif
s. Just chain the if
s:
function(a if A else b if B else c)
Which is equivalent to (as precedence is left to right):
function(a if A else (b if B else c))
Obviously this could get complicated (and over the PEP8 80 char limit), E.G.:
move(N if direction == "N" else E if direction == "E" else S if direction == "S" else W)
In which case the longer form is better:
if direction == "N":
move(N)
elif direction == "E":
move(E)
elif direction == "S":
move(S)
else:
move(W)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With