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:09 and the foreign loop have else clauses that you can attach 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:35 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 runt he 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 the 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:04 and Guido dropped in and said, "You know what,
2:07 I would not have this feature at all if I had to do it over".
2:10 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,
2:17 there are many successful languages that don't have an else clause,
2:20 I think the else clause is confusing,
2:22 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:29 if you break out the loop early in this case, the else clause does not run,
2:32 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.