Just for completeness, here are a few single-line if / else block alternatives:
msg = 'foo' if foo else 'bar' msg = foo and 'foo' or 'bar' msg = ('bar', 'foo')[bool(foo)]
The first one is by far the clearest, if you don't like the single line font, I would suggest using your second method or thagorn's answer. A call to bool() is only necessary in the latter case, if foo no longer bool (or 0/1).
Obviously, in your example function, you can simply return this immediately without even using the msg variable:
def foobar(): return 'foo' if foo else 'bar'
source share