If/then style question
Steven D'Aprano
steve+comp.lang.python at pearwood.info
Sat Dec 18 00:50:46 EST 2010
On Fri, 17 Dec 2010 10:53:45 -0500, Steve Holden wrote about for...else:
> This construct appears to be unpopular in actual use, and when it comes
> up in classes and seminars there is always interesting debate as people
> discuss potential uses and realise there are useful applications.
Yes, I find I don't need it often, but it is useful from time to time.
I wonder whether it would have been more useful to reverse the sense of
the else, and have it run only if the for loop *didn't* run to
completion. That seemed more intuitive to me, and I've wanted to do this
more than once. Here's a toy example:
for x in sequence:
if x == "spam":
print("exiting early")
break
elif x == "ham":
print("exiting early")
break
do_something(x)
would become:
for x in sequence:
if x == "spam":
break
elif x == "ham":
break
do_something(x)
else:
print("exiting early")
> I think the choice of keyword is probably not Guido's crowning language
> achievement, but then since the English keywords don't make natural
> sense to those who speak other languages it's at least fair that there
> should be one that isn't totally natural to English speakers. A small
> price to pay for all the other keywords not being Dutch.
Indeed :)
--
Steven
More information about the Python-list
mailing list