Normal case:
do_something if condition_met else do_something_else
My case:
do_something if condition_met elif do_something_1 else do_something_else
Normal case:
do_something if condition_met else do_something_else
My case:
do_something if condition_met elif do_something_1 else do_something_else
You can chain these ternaries in python, however, I would not recommend it, I find it difficult to read.
Compare
a = True
b = False
c = 1 if a else 2 if b else 3
to
if a:
c = 1
elif b:
c = 2
else:
c = 3
What is more easy to read? Is it worth it to cramp this into one line?
This isn't something that's easily doable, although there might be some convoluted way to accomplish this. However, is there really any benefit to using a ternary in this case over a simple if..else
block, i.e.
if condition_met:
do_something
else:
print(something)
It's somewhat common in Python for there to exist a convoluted one-liner to accomplish what you want that's much less readable than doing it the normal way, and in those cases it's almost always better to take the more readable approach.
You can chain these together much the same way you would do with the ternary operator in C
print ('a' if False else 'b' if False else 'c')
This is effectively equivalent to
if False:
char = 'a'
else:
if False:
char = 'b'
else:
char = 'c'
print (char)