0

I have two code snippets which essentially do the same thing. Here are the two samples :

retval = count or num_element > 0

and

if count or num_element > 0 :
    retval = True
else:
    retval = False

Is there any specific reason the first option is preferred over the second? I know that people have their own choices. But I am curious to know if there are any performance or other implications. I feel second way is more readable.

1
  • I would argue that 2nd case is way more readable. It's just an opinion, but yes it is, but I find it made idiot-readable. I think anyone who can read the language should have no big problems with the 1st code. With the 1st form you get 4 to 1 compression in lines of code, and in case of more than 2-3 such tests in a row, I would rather like to read 5 lines of 1st example than 20 lines of if-elses. Maybe that was it, also it's less typing =). Commented May 11, 2014 at 21:41

2 Answers 2

10

They are not essentially the same. If count is a true value (non-zero, or not an empty container, etc.) then the value of count is assigned in the first form. The second form always assigns a boolean.

Unless the statement is part of a tight high-iteration-count loop, performance shouldn't be an issue. Even if it is part of a loop, the difference will be minute.

What remains is a stylistic choice, but your second form is usually extremely redundant.

Whenever I see a if test: value = True, else: value = False statement, it is usually a sign that someone hasn't understood that the test itself is already returning a boolean or can be made into one.

If you need a true boolean value, use:

retval = bool(count) or num_element > 0

otherwise just stick with the first option.

Sign up to request clarification or add additional context in comments.

4 Comments

@Martjin: My bad. I have not posted my question correctly. I have edited it. However, your answer was spot on for my incomplete question.
Wouldn't it be neater to say retval = bool(count or num_element > 0)?
@kqr: only if num_element is a custom class that returns something other than a boolean from the __gt__ method would it a make difference here.
@MartijnPieters I understand it wouldn't make a difference in most cases, I'm just suggesting it as an improvement – in part due to what you're saying and in part because it follows the general pattern also represented in things like stuff = set([1, 2, 3] * 4) where the entire RHS is wrapped.
2

If you specifically wanted retval to wind up as either True or False (rather than possibly the value of count if that value is truthy), you could do the following:

retval = True if count else num_element > 0

2 Comments

I could be wrong here but if count were to be a false value then retval would also be false, no matter what num_element was.
I misremembered the original expression and thought it was an and. Fixed now.

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.