Can you spot the bug?

Take a look at the following snippet:

def foo(flag):
return "hello", "world" if flag else "friend"

Now, here are two possible evaluations. which one is correct?

> foo(True)
("hello", "world")

> foo(False)
"friend"

> foo(True)
("hello", "world")

> foo(False)
("hello", "friend")

In python the "hello", "world" statement is implicitly evaluated as a tuple, which causes the confusion. foo's statement can be interpreted in two different ways:

# option A
("hello", "world") if flag else "friend"
# option B
"hello", ("world" if flag else "friend")

So which one is correct? Option B -

> foo(True)
("hello", "world")

> foo(False)
("hello", "friend")

Takeaways? Explicit is better than implicit , and of course: