1

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
tenbeh5
  • 27
  • 6
  • 1
    If you really want it on the next line (instead of using [yapf](https://github.com/google/yapf), [autopep8](https://github.com/hhatto/autopep8), or [Black](https://github.com/ambv/black)—I strongly recommend using one of these, my personal preference is yapf), you'll need to wrap the entire thing in a parentheses, or use a backslash, to indicate the next line follows the current one: see https://stackoverflow.com/q/4172448/500207 – Ahmed Fasih Apr 19 '18 at 03:10
  • @AChampion you are right, I have a question which requires me to use ternary operators for 3 different conditions which involves if, elif, else. This is the solution I can come out with. The next line will have another ternary operator. – tenbeh5 Apr 19 '18 at 03:14

3 Answers3

0

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?

MaxNoe
  • 14,470
  • 3
  • 41
  • 46
-1

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.

amiller27
  • 491
  • 5
  • 8
  • I gotcha. Well, I am doing this because I have a question which requires me to use ternary operators for 3 different conditions which involves if, elif, else. This is the solution I can come out with. The next line will have another ternary operator. – tenbeh5 Apr 19 '18 at 03:15
  • Can you nest them? I.e. something like `do_thing1 if condition1 else (do_thing2 if condition2 else do_thing3)`? – amiller27 Apr 19 '18 at 03:17
-1

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)
TallChuck
  • 1,725
  • 11
  • 28
  • The question is marked python-3.x, it's 2018 and you answer using pyhton 2. – MaxNoe Apr 20 '18 at 03:05
  • @MaxNoe tell RedHat to start supporting python 3, I've edited the answer by the way, would you remove your down vote? – TallChuck Apr 20 '18 at 03:07
  • https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/html/7.5_release_notes/chap-red_hat_enterprise_linux-7.5_release_notes-deprecated_functionality – MaxNoe Apr 20 '18 at 03:08
  • It's not about the question, the question is totally fine. Giving a python 2 answer to a question tagged python 3 and than rumbling about red hat is what is not fine. – MaxNoe Apr 20 '18 at 22:42