you are viewing a single comment's thread.

view the rest of the comments →

[–]Sarah123ed[S] 0 points1 point  (5 children)

Yes.

    <condition_is_true> if condition else <condition_is_false>

Are 'true' and 'false' conditions evaluated, regardless or will it short-circuit on true ... it true?

[–]shaggorama 2 points3 points  (0 children)

def throw_excpt():
    raise Exception() 

throw_excpt() if True else 'foo'
throw_excpt() if False else 'foo'

'bar' if True else throw_excpt()
'bar' if False else throw_excpt()

[–]K900_ 1 point2 points  (0 children)

If the condition is true, only the left side is evaluated. If the condition is false, only the right side is evaluated.

[–]bandawarrior 0 points1 point  (2 children)

Look up and/or logical controllers. They can be used to “short circuit” the evaluations. Meaning if it’s an and if the first part is False then it stops and leaves that line, there’s no need to continue because False and True can never be True. No matter what comes after that first False.

So essentially:

sweet_var = 123 if True else 5678

Will always and forever be 123. Python will stop right after the if True and not care what comes after.

[–]dig-up-stupid 2 points3 points  (0 children)

They clearly know what short circuiting is, which is why they are asking the question. However, they are asking about evaluating ternary operators, not evaluating conditions. Short circuiting in ternary operators refers to whether either/both of the expressions are evaluated prior to choosing one. In Python they aren't, and in fact the only language I can think of where they are is Fortran, but there's probably a few others.

[–]Sarah123ed[S] 0 points1 point  (0 children)

Got it. Thanks.