[Python-ideas] for/else syntax
Jim Jewett
jimjjewett at gmail.com
Fri Oct 2 16:03:53 CEST 2009
On Thu, Oct 1, 2009 at 1:17 AM, Stephen J. Turnbull <stephen at xemacs.org> wrote:
> Note: As a native speaker, since "for ... else" isn't English, I would
> certainly look up the semantics before using the syntax. This is very
> probably true for Japanese as well, who are far better at (consciously)
> remembering English syntax than native speakers are. I don't know
> about other non-natives.
As another native speaker, I do find it confusing on most for loops --
so I don't use it there. It does make sense on search loops.
Normal loop, where it is misleading:
for item in source:
process(item)
else:
# yikes, I would wrongly expect this to mean empty.
But search loop where it makes sense:
for candidate in source:
if test(candidate):
choice=candidate
break
else:
choice=default
(That said, even then, I tend to set the default outside the loop
construct, just to avoid the else confusion.)
-jJ
More information about the Python-ideas
mailing list