Write Pythonic Code Like a Seasoned Developer Transcripts
Chapter: Pythonic Loops
Lecture: Loops have an else block, don't use it
0:01 One peculiar feature of Python loops is that they have an "else" block, first of all, do you know that the "while" loop
0:09 and the "for...in" loop have "else" clauses that you can attach to them? Second, can you remember the order in which these happen,
0:16 what triggers the "else" block to run versus it not running? Let's have a look, so here we have two "while" loops, one "while" loop runs to completion;
0:26 the other "while" loop breaks out early, let's just run to see the output. Cool, so here we have 5 dots and here we have 4 dots,
0:33 that means we've run through one all the way to the end, the other we stopped a little bit early.
0:38 So, there is nothing special about that, but let's see about the "else" clause here,
0:42 so let's say "for all loops" we can say "else", so "do the loop, else", we'll just put a message to know that it ran or didn't in the "else" clause,
0:53 so we'll write something like this, "in the else clause of the whole loop",
0:57 and down here we'll say "else", in the "else" clause of the early break loop, which one is going to run? Let's find out.
1:07 OK, so if we run through the entire loop, and we go all the way to the end, this becomes False, and then "else", we run this.
1:16 On the other hand, if we go through here and we never go to a case where this is no longer True
1:21 but we break out early instead, we don't run the "else" clause. I am sure there is a few good uses for this, somewhere,
1:27 it doesn't seem like a language feature that's worth it to me, but my recommendation for Pythonic code is: Do not use the "else" clause on loops.
1:36 Yeah, here is a whole language feature, I say don't use it, now you might say, "Michael, is that really Pythonic? I mean it is a language feature,
1:43 who would say "Don't use a language feature that was put in there by Guido Van Rossum, the creator of Python himself?"" let's see.
1:52 Back in 2009, somebody asked about the "for/while/else" without "break" or "return" clauses and Guido dropped in and the guy said,
2:01 "Well, I am not sure that this is a great choice of words", and Guido dropped in and said, "You know what,
2:05 I would not have this feature at all if I had to do it over". "I would not put the "else" clause on loops in the language, period."
2:13 That to me is a pretty strong statement that you know, we don't really need this, there are many successful languages that don't have an "else" clause,
2:19 I think the "else" clause is confusing, Guido thinks it's probably not the best to put it in there, let's all agree to avoid it.
2:27 Right, so we saw that we have "else" clauses here, if you break out the loop early in this case, the "else" clause does not run,
2:34 if we run the loop to completion, basically to a False state, then it will run. "I would not have put the "else" clause in the language at all
2:43 if I had to do it over", to me it sounds like non-Pythonic.