On 10/10/2022 15:52, Weatherby,Gerard wrote:
Core developer Raymond Hettinger explains the history starting at 15:40
https://www.youtube.com/watch?v=OSGv2VnC0go
(which I found on stackoverflow
https://stackoverflow.com/questions/9979970/why-does-python-use-else-after-for-and-while-loops
)
TL:DR
The “else” is a historical artificial from the way developers thought during
the transition from unstructured (i.e. “GOTO”) programming to structured
programming. Since we all do structured now, it seems odd.
From: Python-list <python-list-bounces+gweatherby=uchc....@python.org> on behalf of
Calvin Spealman <cspea...@redhat.com>
Date: Monday, October 10, 2022 at 10:38 AM
To: python-list@python.org <python-list@python.org>
Subject: Re: for -- else: what was the motivation?
*** Attention: This is an external email. Use caution responding, opening
attachments or clicking on links. ***
On Sat, Oct 8, 2022 at 5:35 PM rbowman <bow...@montana.com> wrote:
On 10/7/22 21:32, Axy wrote:
So, seriously, why they needed else if the following pieces produce same
result? Does anyone know or remember their motivation?
In real scenarios there would be more logic in the for block that would
meet a condition and break out of the loop. If the condition is never
met, the else block runs. To steal from w3schools:
fruits = ["apple", "peach", "cherry"]
for x in fruits:
print(x)
if x == "banana":
break
else:
print("Yes we got no bananas")
I wonder if for/else could have been less confusing if it was referred to
as for-break-else and if the else clause was only valid syntax if the for
loop actually contained a break statement in the first place.
Sounds reasonable. It would be something alike UnboundLocalError when a
local variable referenced before assignment. If they won't remove "else"
completely in far future, that checking really worths implementing now.
Excellent stackoverflow link, thanks!
Axy.
--
https://mail.python.org/mailman/listinfo/python-list