for / while else doesn't make sense

Ned Batchelder ned at nedbatchelder.com
Sat Jun 4 07:37:01 EDT 2016


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? If this appeared in a code
review, first we'd have a conversation about what this code was
meant to do, and then I would ask, "Why aren't you using a for loop?"
I suspect most Python programmers would be similarly confused about
why you aren't using one of the central constructs of the language.

What's next? "Why have both if and while? A goto will work for both!"

--Ned.



More information about the Python-list mailing list