for / while else doesn't make sense

Marko Rauhamaa marko at pacujo.net
Sun Jun 5 07:43:07 EDT 2016


Ned Batchelder <ned at nedbatchelder.com>:

> On Saturday, June 4, 2016 at 11:29:30 PM UTC-4, Lawrence D’Oliveiro wrote:
>> > >     item_iter = iter(items)
>> > >     while True :
>> > >         item = next(item_iter, None)
>> > >         if item == None :
>> > >             break
>> > >         if is_what_i_want(item) :
>> > >             break
>> > >     #end while
>
> OK. The best I can say is that you seem to think very differently
> about your code than I do. Re-implementing the logic of iteration just
> so you can make the two end conditions look similar seems like a very
> bad trade-off to me. It adds more lines, and more names, and as Steven
> points out, more opportunities to introduce errors.

I often experiment with different loop constructs to find the one most
pleasing to the eye. Working directly off iterators is quite rare but a
while-vs-for consideration is frequent. Also, should the stepping part
be in the beginning, middle or end of the loop body?

> I'm not sure what part of Python you are putting in the "bad"
> category. This example didn't involve for/else, so are you saying that
> break statements inside for-loops are a bad part?

It takes time to acclimatize to a new programming language. Initially,
you tend to shun odd-looking constructs like comprehensions. Then, you
might overdo constructs like lambdas and maps. Eventually, you'll find a
style the suits both you and the language.


Marko



More information about the Python-list mailing list