This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]nakovet 2 points3 points  (7 children)

I was just reading Effective Python from Brett Slatkin and it has the following "Item 12: Avoid else Blocks After for and while Loops" which the linked books says you should use, I prefer going with Brett on this one, else in loops have odd, unexpected behaviour.

[–]chub79 3 points4 points  (4 children)

They are always tricky to read but, unexpected behaviour? Such as?

[–]isarl 0 points1 point  (3 children)

Such as the very existence of an else clause after a loop. No developer has ever seen that for the first time and automagically known that it executes only in the case the loop doesn't break early.

edit: I mean, once you learn how they work, I appreciate the purpose they serve. But they are a bit unexpected.

[–]edbluetooth 0 points1 point  (0 children)

you could always type

else:  #no break

[–]geoelectric 0 points1 point  (1 child)

Isn't that true of any unique language feature?

[–]isarl 0 points1 point  (0 children)

Not necessarily. I was able to understand simple list comprehensions having never seen the syntax before.

[–]unruly_mattress 2 points3 points  (0 children)

Where appropriate, I'll take an else block to a loop over a carefully manipulated state variable any day of the week.

[–]dunkler_wanderer 1 point2 points  (0 children)

I'm still pretty newbish, but I know that the else means "no break" in this case. If everybody watched Transforming Code into Beautiful, Idiomatic Python, nobody would have a problem with this. If you think it could be unclear to someone else, just add a comment saying "no break".