A friend of mine just introduced me to the Eclipse IDE, which may or may not end up replacing IDLE for me.

Anyway, one of the features of Eclipse is error warnings as the code is *displayed*, rather than waiting for errors to pop up at runtime.

This code got flagged:

if self.tag == None:
   return ""
elif self.tag == "_text":   <-----
   return "\t"*tablvl + self.value
elif self.tag == "_toplevel":
   s = ""
   for i in self.children:
      s = s + "\n" + i.__str__(0)
   return s

the line marked <----- got flagged as "unnecessary else", since the return "" technically prevents the code from getting to 'elif self.tag == "_text":' unless the if falls through.

Stylistically, what is your opinion? Would you write the code like that, or like this:

if self.tag == None:
    return ""
 if self.tag == "_text":   <-----
    return "\t"*tablvl + self.value
 if self.tag == "_toplevel":
    s = ""
    for i in self.children:
       s = s + "\n" + i.__str__(0)
    return s

Lint Filters give warnings that are not always sensible. Using elif instead of all if is faster, since Python has to evaluate all the if, but stops at the first true elif condition.

In your case it doesn't make much difference since you have a return statement, which will stop Python from going through the rest of the if conditionals. So 'if' is less to type than 'elif'.

Thanks. My feeling was that the first was more human-readable. Do you agree?

I agree! Since you are just going down the line with the same variable, if/elif makes more sense IMHO. The if/elif ties the conditional together.

This article has been dead for over six months. Start a new discussion instead.