13

I have the following code (minus some other operations):

def foobar():
    msg=None
    if foo:
        msg='foo'
    else:
        msg='bar'
    return msg

Is the following better practice for the msg variable?

def foobar():
    if foo:
       msg='foo'
    else:
       msg='bar'
    return msg

I'm aware that I could simplify the above functions to ternary expressions, however there are operations in each if-else block that I've left out.

0

7 Answers 7

8

Either should be fine but I would probably do:

def foobar():
    msg='bar'
    if foo:
        msg='foo'
    return msg
Sign up to request clarification or add additional context in comments.

1 Comment

There are a very minor performance impact to the code, it is assigning msg twice for if foo case. In normal practice use shouldn't be any issue, but building anything performance critical this solution might not be the best.
7

Just for completeness, here are some one-line alternatives to if/else blocks:

msg = 'foo' if foo else 'bar'
msg = foo and 'foo' or 'bar'
msg = ('bar', 'foo')[bool(foo)]

The first of those is definitely the most clear, if you don't like the one-liner I would suggest using your second method or thagorn's answer. The bool() call is only necessary in the last one if foo is not already a bool (or 0/1).

Obviously in your example function you could just return this immediately without even using a msg variable:

def foobar():
    return 'foo' if foo else 'bar'

1 Comment

Good and comprehensive advice overall, but they stated that there's more processing in the conditional blocks than just that.
3

In Python there's no great advantage to initializing before a conditional as in your first example. You just need to be sure that the variable is initialized before it's returned. That assumes (based on your examples) that you're using the "single exit point" paradigm. In some cases in Python it's appropriate, but other times you get cleaner code by exiting early when possible.

def earlyReturn(mycheck):
  if not mycheck:
    return 'You forgot something.'

  # code here if the test passes without needing an extra level of indentation.

Comments

2

I realize that there are some things left out, but if you don't actually need to manipulate msg, I imagine you could just return the intended contents, without ever needing a variable; return 'foo'

Comments

1

I would definitely say that the later is better. There is no recommendation for Python to initialize variables. Therefor it shall be avoided if it's not adding something of value to the code like a fallback value or makes the code more readible, which it does'nt in this case.

Edit: By fallback value I mean the same as thagorn and mikebabcock has suggested.

Comments

0

If what you've shown is all that msg is involved in, then initializing it doesn't do anything for you, and the second solution is better.

Comments

0

If that's the entire logic, why not do:

def foobar():
    msg='bar'
    if foo:
        msg='foo'
    return msg

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.