Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'value,': 0.03; 'none)': 0.07; 'loop.': 0.09; 'subject:while': 0.09; 'value.': 0.15; 'functools': 0.16; 'n),': 0.16; 'oct': 0.16; 'returned,': 0.16; 'select,': 0.16; 'skips': 0.16; 'subject:expression': 0.16; 'wrote:': 0.17; 'thu,': 0.17; '>>>': 0.18; 'import': 0.21; 'received:mail-bk0-f46.google.com': 0.22; 'demonstrate': 0.23; 'random': 0.24; 'header:In-Reply-To:1': 0.25; 'values': 0.26; 'am,': 0.27; 'received:209.85.214.46': 0.27; 'message- id:@mail.gmail.com': 0.27; 'code': 0.31; 'not.': 0.32; 'goes': 0.33; 'to:addr:python-list': 0.33; 'received:google.com': 0.34; 'received:209.85': 0.35; 'there': 0.35; 'but': 0.36; 'does': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'fact': 0.38; 'to:addr:python.org': 0.39; 'received:209.85.214': 0.39; 'header:Received:5': 0.40; 'thomas': 0.62; 'rachel': 0.84; 'to:name:python': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=PTxX9R81ANNBmniKVgWlG5b6Up3h/bRsWVHIj8VMA6I=; b=L4g0nLKW7dfLUH8IjZV2tO9Qg6uydRTY5vmVHd+JYpBHM6dX2i2EZ9zdbnjiuH9rK0 Aq8H3Vg2WJhARLYIiRTcFFESJpYmOZ6XMcXRZRlOAVL2bgknmtoD/muLUu1gXpCEv1tF +Ep1cMp1xD4NWMUzPhDMlX2G/0lxIjsKx5CXnG5sdqkN8twKfH14kVziW32ziu1NQ4Ad Ba6eyakAIhkIBZPiKNx53PmW5IEbAIiX629uEVJESBiZqGD8yD8dlSEgS0eUzKG8R14Q 8g6linL5uJS/V7a4jui/SU7Les1BGj6J7OuC1T5NkmiN0Ak+HSZNEqktrgraSQqtE734 BfVA== MIME-Version: 1.0 In-Reply-To: References: <50886398.5050301@tim.thechases.com> <7x7gqf1na2.fsf@ruckus.brouhaha.com> From: Ian Kelly Date: Thu, 25 Oct 2012 10:47:40 -0600 Subject: Re: while expression feature proposal To: Python Content-Type: text/plain; charset=ISO-8859-1 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 25 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1351183692 news.xs4all.nl 6965 [2001:888:2000:d::a6]:37473 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:32148 On Thu, Oct 25, 2012 at 10:36 AM, Ian Kelly wrote: > On Thu, Oct 25, 2012 at 1:21 AM, Thomas Rachel > > 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