[Python-ideas] for/else statements considered harmful
Stephen J. Turnbull
stephen at xemacs.org
Thu Jun 7 12:50:14 CEST 2012
Devin Jeanpierre writes:
> On Thu, Jun 7, 2012 at 4:31 AM, Yuval Greenfield <ubershmekel at gmail.com> wrote:
> > I believe using for/else will cause you and maintainers of your
> > code to make more mistakes.
>
> I don't follow. What mistakes would people make? Why would they
> make them?
There was a long thread about a year ago on this list, where a couple
of less experienced programmers and even a couple of people who have
long since proven themselves reliable, gave code examples that
obviously hadn't been tested.<wink/> There's a summary at:
http://grokbase.com/t/python/python-ideas/09abg9k5fc/summary-of-for-else-threads
The reason they make such mistakes is that there's a strong
association of "else" with "if-then-else", and for many people that
seems to be somewhere between totally useless and actively misleading.
For me, there are a number of reasonable mnemonics, a couple given in
this thread, but IIRC the only idiom I found really plausible was
def search_in_iterable(key, iter):
for item in iter:
if item == key:
return some_function_of(item)
else:
return not_found_default
More information about the Python-ideas
mailing list