for / while else doesn't make sense

Ned Batchelder ned at
Sun Jun 5 07:29:24 EDT 2016

On Saturday, June 4, 2016 at 11:29:30 PM UTC-4, Lawrence D’Oliveiro wrote:
> On Saturday, June 4, 2016 at 11:37:18 PM UTC+12, Ned Batchelder wrote:
> > On Friday, June 3, 2016 at 11:43:33 PM UTC-4, Lawrence D’Oliveiro wrote:
> > > On Saturday, June 4, 2016 at 3:00:36 PM UTC+12, Steven D'Aprano wrote:
> > > > You can exit a loop because you have run out of items to process, or you can
> > > > exit the loop because a certain condition has been met.
> > > 
> > > But why should they be expressed differently?
> > > 
> > >     item_iter = iter(items)
> > >     while True :
> > >         item = next(item_iter, None)
> > >         if item == None :
> > >             break
> > >         if is_what_i_want(item) :
> > >             break
> > >     #end while
> > 
> > Do you actually write loops like this?
> Is that a non-trolling question? Yes. All the time.

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.

> When a language has good and bad parts, it behooves the wise programmer to concentrate on the good parts and try to ignore the bad.
> Python’s for-loops have their uses—I *did* point this out too, did you not notice?—but they are best confined to the situations that they are good at.

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?

IIRC, this started as a comparison of Python's for loops and C's.  Do you
also write C for-loops as while statements if they have a break in them,
to make both of those end conditions similar?

I'm not trolling, I'm trying to understand your unusual approach.


More information about the Python-list mailing list