[Python-ideas] for/else statements considered harmful

Yuval Greenfield ubershmekel at gmail.com
Thu Jun 7 13:01:18 CEST 2012


On Thu, Jun 7, 2012 at 1:50 PM, Stephen J. Turnbull <stephen at xemacs.org>wrote:

>    def search_in_iterable(key, iter):
>        for item in iter:
>            if item == key:
>                return some_function_of(item)
>        else:
>            return not_found_default
>
>
You don't need the "else" there. An equivalent:

    def search_in_iterable(key, iter):
       for item in iter:
           if item == key:
               return some_function_of(item)
       return not_found_default


I'm not sure I understood what you meant but I'll assume that by
"plausible"/"reasonable" you meant that it's a good example as to how
for/else is misleading.

Devin Jeanpierre Wrote:

> Also, are you worried about people that read the documentation and
> know what for-else does, or the people that don't or haven't read this
> documentation?


On this issue I'm worried about all sentient programmers.

Yuval
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20120607/ca833d88/attachment.html>


More information about the Python-ideas mailing list