Chris Angelico <ros...@gmail.com> wrote:

> Maybe a for loop isn't the best other example, but I
> frequently work with places where I want to call some function and
> keep iterating with the result of that until it returns false:
> 
> while (var = func())
> {
>     ....
> }
> 
> In Python, that gets a lot clunkier. The most popular way is to turn
> it into an infinite loop:
> 
> while True:
>     var = func()
>     if not var: break
>     ....
> 

My preferred way would be to write it as a `for` loop:

for var in iter(func, False):
   ...


Though you do have to be sure to get the sentinel value correct as it will 
only break for the expected terminal False, not for 0, "", or None.

-- 
Duncan Booth
-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to