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,
0:06 first of all, do you know that the "while" loop
0:08 and the "for...in" loop have "else" clauses that you can attach to them?
0:12 Second, can you remember the order in which these happen,
0:15 what triggers the "else" block to run versus it not running? Let's have a look,
0:20 so here we have two "while" loops, one "while" loop runs to completion;
0:25 the other "while" loop breaks out early, let's just run to see the output.
0:29 Cool, so here we have 5 dots and here we have 4 dots,
0:32 that means we've run through one all the way to the end,
0:34 the other we stopped a little bit early.
0:37 So, there is nothing special about that, but let's see about the "else" clause here,
0:41 so let's say "for all loops" we can say "else", so "do the loop, else",
0:48 we'll just put a message to know that it ran or didn't in the "else" clause,
0:52 so we'll write something like this, "in the else clause of the whole loop",
0:56 and down here we'll say "else", in the "else" clause of the early break loop,
1:00 which one is going to run? Let's find out.
1:06 OK, so if we run through the entire loop,
1:09 and we go all the way to the end, this becomes False, and then "else", we run this.
1:15 On the other hand, if we go through here
1:17 and we never go to a case where this is no longer True
1:20 but we break out early instead, we don't run the "else" clause.
1:23 I am sure there is a few good uses for this, somewhere,
1:26 it doesn't seem like a language feature that's worth it to me,
1:29 but my recommendation for Pythonic code is: Do not use the "else" clause on loops.
1:35 Yeah, here is a whole language feature, I say don't use it, now you might say,
1:39 "Michael, is that really Pythonic? I mean it is a language feature,
1:42 who would say "Don't use a language feature that was put in there
1:46 by Guido Van Rossum, the creator of Python himself?"" let's see.
1:51 Back in 2009, somebody asked about the "for/while/else"
1:56 without "break" or "return" clauses and Guido dropped in and the guy said,
2:00 "Well, I am not sure that this is a great choice of words",
2:03 and Guido dropped in and said, "You know what,
2:04 I would not have this feature at all if I had to do it over".
2:07 "I would not put the "else" clause on loops in the language, period."
2:12 That to me is a pretty strong statement that you know, we don't really need this,
2:15 there are many successful languages that don't have an "else" clause,
2:18 I think the "else" clause is confusing,
2:20 Guido thinks it's probably not the best to put it in there, let's all agree to avoid it.
2:26 Right, so we saw that we have "else" clauses here,
2:28 if you break out the loop early in this case, the "else" clause does not run,
2:33 if we run the loop to completion, basically to a False state, then it will run.
2:38 "I would not have put the "else" clause in the language at all
2:42 if I had to do it over", to me it sounds like non-Pythonic.