Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!selfless.tophat.at!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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'thread,': 0.04; 'kelly': 0.09; 'none)': 0.09; 'none:': 0.09; 'subject:()': 0.09; 'subject:don': 0.09; 'sun,': 0.09; 'def': 0.13; 'am,': 0.14; 'wrote:': 0.14; 'none"': 0.16; 'pythonic': 0.16; 'send()': 0.16; 'work,': 0.20; 'header:In-Reply-To:1': 0.22; 'loop': 0.22; 'right.': 0.22; '(and': 0.22; 'received:209.85.214.174': 0.23; 'received:mail-iw0-f174.google.com': 0.23; 'instead': 0.26; 'chris': 0.27; 'work.': 0.27; 'message-id:@mail.gmail.com': 0.28; 'ignored.': 0.29; "won't": 0.30; 'second': 0.31; 'to:addr:python- list': 0.32; 'quite': 0.36; 'some': 0.37; 'should': 0.37; 'received:209.85': 0.37; 'received:google.com': 0.38; 'received:209.85.214': 0.39; 'to:addr:python.org': 0.39; 'received:209': 0.39; 'header:Received:5': 0.40; 'twice': 0.60; '2011': 0.62; 'legal': 0.64; 'subject:skip:g 10': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:in-reply-to:references:date :message-id:subject:from:to:content-type:content-transfer-encoding; bh=sYNOU5PbWKpTtn/pbxKVAZ1GViKvezAWqNnDGknY/RQ=; b=tkpoIeWdPw7MD6NIXvhKeGyQpCN7LlXetM2np7S04cuFMz+wF11ckj9dQDBhbtwROy HyfaN2jRvj96ms3ZvG/07nDTbxoljfBk0fJSJTrHXYM8TihLbvuy/uWhZMyXEib5VViI itsGcnnXw1nAtLFRKxQueuvK3JMopl33KnKR8= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type:content-transfer-encoding; b=N+FouFHGyIYxgXKj/7ZOqCUCHlpDGDuKtOueYZjwd1/G7Is+uF0Ko8fMwPMG4s72AK jCu4ByVaziIS2D26EcIWVZQYK5kVZzbQq7EXMzxO7AKxDbJJ3oV1j0CMpS4Bjnb+WQyh DUF6km4Vwh5HKIKcZZIYukYE/UAIpitZXt9ms= MIME-Version: 1.0 In-Reply-To: References: <1k19ubi.dwyvio12tkc4kN%see@sig.for.address> Date: Sun, 15 May 2011 11:17:31 +1000 Subject: Re: I don't understand generator.send() From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 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: 26 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1305422254 news.xs4all.nl 65870 [::ffff:82.94.164.166]:47962 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:5401 On Sun, May 15, 2011 at 11:05 AM, Ian Kelly wrote: > Actually, this won't work, because the value of the "yield None" gets > ignored. =A0Thus if you try to call send() twice in a row, the generator > the treats second send() as if it were a next(), and it is not > possible to have more than one item in the queue. You're right. It needs a while loop instead of the if (and some slight reordering): def ints(): i=3D0 queue=3D[] while True: if queue: # see other thread, this IS legal and pythonic and quite sensible sent=3D(yield queue.pop(0)) else: sent=3D(yield i) i+=3D1 while sent is not None: queue.append(sent) sent=3D(yield None) # This is the return value from gen.send() That should work. Chris Angelico