Ifs and assignments
Duncan Booth
duncan.booth at invalid.invalid
Fri Jan 3 10:46:50 EST 2014
Chris Angelico <rosuav at 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
More information about the Python-list
mailing list