On Fri, Dec 4, 2020 at 2:42 AM Jonathan Fine
Hi
On Thu, Dec 3, 2020 at 2:02 PM Chris Angelico
wrote: well... uhhh.... Technically you can do that already....
for a in aaa: for b in bbb: if condition(a, b): break else: continue # We didn't break from b, so continue a break # We did break b, so break a
This is ingenious. Thank you. Here the "else ... continue" block is for normal exit, and the suite of statements containing the break statement is for unusual exit.
But I don't think anyone's actually doing that :)
We're sort of using the 'break ... continue' as a goto, that goes to the suite after the else block. And though not in name a goto, it has the same problems as a honest goto statement.
I consider your example, Chris, to be an argument in favour of allowing 'for ... if break ... else ...'.
Well, it's a toy example, so it shouldn't be considered a very strong
argument. But if this IS actually being done in production, then yes,
that's an argument in favour...
On Fri, Dec 4, 2020 at 2:50 AM Serhiy Storchaka
I did. :(
In cases when functions or generators add significant overhead.
... and that's what Serhiy is saying. Personally, I'd be more inclined to "break 2" or "break somelabel" than a keyword for "do this if we broke out of the loop". It's not often that you actually need to say "do this if we broke out of the loop" unless the "do this" part is another break statement (otherwise, you'd just put the same code up next to the break statement); so it'd be more logical to have a way to say "break out of these two loops". ChrisA