Behavior of the for-else construct
Rob Cliffe
rob.cliffe at btinternet.com
Thu Mar 3 19:13:00 EST 2022
I find it so hard to remember what `for ... else` means that on the very
few occasions I have used it, I ALWAYS put a comment alongside/below the
`else` to remind myself (and anyone else unfortunate enough to read my
code) what triggers it, e.g.
for item in search_list:
...
... break
else: # if no item in search_list matched the criteria
You get the idea.
If I really want to remember what this construct means, I remind myself
that `else` here really means `no break`. Would have been better if it
had been spelt `nobreak` or similar in the first place.
Rob Cliffe
On 03/03/2022 23:07, Avi Gross via Python-list wrote:
> The drumbeat I keep hearing is that some people hear/see the same word as implying something else. ELSE is ambiguous in the context it is used.
>
> And naturally, since nobody desperately wants to use non-reserved keywords, nobody seems ready to use a word like INSTEAD instead.
>
> Ideally, a language should be extendable and some languages like R allow you to place all kinds of things inside percent signs to make new operators like %*% or %PIPE% ...
>
> Just because some feature may be wanted is not a good reason to overly complicate a language. Can you imagine how hard it would be both to implement and read something like:
>
> ...
> ELSE:
> ...
> OK:
> ...
> FINALLY:
> ...
> ULTIMATELY:
> ...
>
> What if multiple of things like the above example need to be triggered in some particular order?
>
> I have to wonder if some new form of wrapper might have made as much sense as in you wrap your loop in something that sets up and traps various signals that are then produced under conditions specified such as the loop not being entered as the starting condition is sort of null, or an exit due to a break or simply because the code itself throws that signal to be caught ...
>
> This reminds me a bit of how some programs add so much functionality because someone thought of it without wondering if anyone (including the ones who sponsored it) would ever want to use it or remember it is there or how. I recall how a version of emacs had a transpose-letter function so after typing "teh" you could hit control-t and a little mock LISP macro would go back and co a cut and go forward and do a paste and leave the cursor where it was. That was sometimes useful, but often just as easy to backspace and retype. But I recall gleefully adding a transpose for words, sentences, paragraphs and was going to add more but I was running out of keystrokes to bind them to and besides it can be fairly easy to select items and yank them and move to where you want them and replace them.
>
> To make changes in a language is sometimes really expensive but also dangerous. A "free" language must be added to sparingly and with so many requests, perhaps only a few non bug-fixes can seriously be considered.
>
>
>
> -----Original Message-----
> From: Akkana Peck <akkana at shallowsky.com>
> To: python-list at python.org
> Sent: Thu, Mar 3, 2022 5:33 pm
> Subject: Re: Behavior of the for-else construct
>
> computermaster360 writes:
>> I want to make a little survey here.
>>
>> Do you find the for-else construct useful?
> No.
>
>> Have you used it in practice?
> Once or twice, but ended up removing it, see below.
>
>> Do you even know how it works, or that there is such a thing in Python?
> I always have to look it up, because to my mind, "else" implies
> it does something quite different from what it actually does.
>
> Which means that even if I worked hard at memorizing what it does,
> so I didn't have to look it up, I still wouldn't use it in code,
> because I want my code to be easily readable (including by future-me).
> for..else makes code difficult to understand by anyone who doesn't
> use for..else frequently: they might be misled into misunderstanding
> the control flow.
>
> ...Akkana
>
More information about the Python-list
mailing list