0

Does someone have a simple answer as to why you would use a If If rather than an If Elif?

Example:

def fun(item):
    if item[0]<'M':
        return 0
    if item[0]<'Q':
        return 1
    return 2
1
  • If you need all the if statements to be checked and not just the first. If you only have a return statement in the if statement, probably never. Commented Dec 24, 2018 at 18:32

1 Answer 1

-1

With a setup like this, where you are returning a value inside each if block, the if statements behave exactly the same as an if elif structure since the return exits out of the function without checking the conditions below. So, when the functionality is the same, the choice should be made on readability. explicit is better than implicit.

There is no good reason to use if statements like this implying exclusively in the conditionals, when if elif statements makes the exclusivity explicit and is much more readable and easier to maintain.

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

Comments