News for hackers. Stuff that matters.
Re:The for/Else Statement in Python by bzb3 on Thursday July 09, 2020 @09:40AM (#23780042)
Surprising. I didn't know this construct and I would naturally assume that the else part would execute if the loop never spun (because the list/etc was empty)
Re:The for/Else Statement in Python by amitutk on Thursday July 09, 2020 @10:12AM (#23780436)
Yes, the else keyword is not intuitive. The best way to remember for/else is for/nobreak
Re:The for/Else Statement in Python by neverartful on Thursday July 09, 2020 @11:21AM (#23781222)
Right! nobreak would make it much clearer.
Re:The for/Else Statement in Python by Dagger2 on Friday July 10, 2020 @01:20AM (#23788396)
It would involve adding a new keyword to the language though. I imagine there was some desire to avoid that.
Re:The for/Else Statement in Python by neverartful on Thursday July 09, 2020 @10:06AM (#23780362)
This was my guess too.
Re:The for/Else Statement in Python by Doxin on Thursday July 09, 2020 @09:57AM (#23780266)
Relatedly there's also a try/else, where the else block only runs if there wasn't an exception.

All these else variants come in handy from time to time.

Re:The for/Else Statement in Python by rosstex on Thursday July 09, 2020 @12:11PM (#23781740)
The most common use case of for/else for me is:

for/len==0