while expression feature proposal

Thomas Rachel nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915 at spamschutz.glglgl.de
Thu Oct 25 06:12:31 EDT 2012


Am 25.10.2012 09:21 schrieb Thomas Rachel:

> I think
>
> # iterate ad inf., because partial never returns None:
> i1 = iter(partial(randrange, n), None)
> # take the next value, make it None for breaking:
> i2 = (j if j in selected else None for j in i1)
> # and now, break on None:
> i3 = iter(lambda: next(i2), None)
>
> would do the job.

But, as I read it now again, it might be cleaner to create an own 
generator function, such as

def rand_values(randrange, n, selected):
     # maybe: selected = set(selected) for the "not in"
     while True:
         val = partial(randrange, n)
         if val not in selected: break
         yield val

for value in rand_values(...):

or, for the general case proposed some posings ago:

def while_values(func, *a, **k):
     while True:
         val = func(*a, **k):
         if not val: break
         yield val

Thomas



More information about the Python-list mailing list