On Thu, Oct 25, 2012 at 10:36 AM, Ian Kelly <ian.g.ke...@gmail.com> wrote: > On Thu, Oct 25, 2012 at 1:21 AM, Thomas Rachel > <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa...@spamschutz.glglgl.de> > wrote: >>> j = next(j for j in iter(partial(randrange, n), None) if j not in >>> selected) >> >> >> This generator never ends. If it meets a non-matching value, it just skips >> it and goes on. > > next() only returns one value. After it is returned, the generator is > discarded, whether it has ended or not. If there were no valid values > for randrange to select, then it would descend into an infinite loop. > But then, so would the dropwhile and the original while loop.
To demonstrate that the code does in fact return: >>> selected = set(range(5)) >>> n = 10 >>> from functools import partial >>> from random import randrange >>> j = next(j for j in iter(partial(randrange, n), None) if j not in selected) >>> j 5 -- http://mail.python.org/mailman/listinfo/python-list