
16-07-2009 o 00:54 Chris Rebert pyideas@rebertia.com wrote:
You can fix that by just writing it as:
while True: SOME ACTIONS HERE if not CONDITION: break
Yeah, but it's not the same :) because eyes must look for the actual loop condition somewhere-within-the-loop (after all, "while True" is common idiom with large field of usage, not only in such situations...).
Another solution I invented for myself is to use object which once evaluates to True, then always to False, e.g.:
from itertools import chain, repeat class FirstTrue(object): def __init__(self): self._iter = chain([True], repeat(False)) def __nonzero__(self): # __bool__ for Py3k return next(self._iter)
Usage:
first = FirstTrue() while first or CONDITION: SOME ACTIONS HERE
or, if we want to *evaluate* CONDITION also at first time:
first = FirstTrue() while CONDITION or first: SOME ACTIONS HERE
Wouldn't be nice to have such factory as built-in or in itertools?
Best regards, *j