Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'subject:: [': 0.04; 'syntax': 0.04; 'subject:Python': 0.06; 'context': 0.07; 'element': 0.07; 'none,': 0.07; 'returned.': 0.07; '[0,': 0.09; 'assigning': 0.09; 'function,': 0.09; 'integers': 0.09; 'pep': 0.09; 'subject:skip:c 10': 0.09; 'wrong,': 0.09; 'python': 0.11; 'def': 0.12; 'assume': 0.14; '2.7': 0.14; '[none,': 0.16; 'assigns': 0.16; 'collects': 0.16; 'comp': 0.16; 'deletes': 0.16; 'foo():': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'none]': 0.16; 'otherwise:': 0.16; 'subject:ideas': 0.16; 'subject:yield': 0.16; 'weird': 0.16; 'ignore': 0.16; 'wrote:': 0.18; 'wed,': 0.18; 'subject:] ': 0.20; '>>>': 0.22; 'example': 0.22; 'creating': 0.23; '(or': 0.24; "i've": 0.25; 'equivalent': 0.26; 'this:': 0.26; 'second': 0.26; 'asking': 0.27; 'values': 0.27; 'header:In-Reply-To:1': 0.27; 'point': 0.28; 'correct': 0.29; 'am,': 0.29; 'statement': 0.30; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'code': 0.31; '"",': 0.31; 'prints': 0.31; 'yields': 0.31; 'file': 0.32; 'class': 0.32; 'another': 0.32; '(most': 0.33; 'third': 0.33; 'skip:_ 10': 0.34; 'no,': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'described': 0.36; 'yield': 0.36; 'doing': 0.36; 'should': 0.36; 'two': 0.37; 'list': 0.37; 'list.': 0.37; 'nov': 0.38; 'to:addr:python-list': 0.38; 'list,': 0.38; 'that,': 0.38; 'recent': 0.39; 'to:addr:python.org': 0.39; 'even': 0.60; 'first': 0.61; 'such': 0.63; 'believe': 0.68; 'surrounding': 0.68; '_it_': 0.84; 'confusing': 0.84; 'describes': 0.84; 'seldom': 0.84; 'subject:Using': 0.84; 'technically': 0.84; 'thing,': 0.91; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=/mrNGCnuIw7XPQB68lIJbfyqevOlVXBhU/Hq930hptY=; b=ewsVCHKXoSayfB370O4jxA62GULEaeHih6XBwvjfW35ffJW3c+hjHMVTCB5qMM+l2v AddUhmp0yEfTb3ywwtwwxmabXycPpoCIztgzUrlzgo5cuyRqMWO1LBGRaPbl5EOoqx6D zaxfpDcQx725/lDFYXSZNjHAOcYS1UG/Pz92oumW0tjFARFzIOqoAXW0zF8IYngCVMUn C2dTMfj+0ngX0yCKBZVKqvuDlqhg1deHfOTnwlXTZedC4yGmc4uIEUAEc8LQmfheXxq5 1+FABZHm3i6PY/Punw2erkSYTwMCjT4wCj+5/MaqRDfOmySYHaSHU0dj5EWOVJ7OYzKG fBSg== MIME-Version: 1.0 X-Received: by 10.66.122.66 with SMTP id lq2mr4840739pab.183.1385476001254; Tue, 26 Nov 2013 06:26:41 -0800 (PST) In-Reply-To: References: Date: Wed, 27 Nov 2013 01:26:41 +1100 Subject: Re: [Python-ideas] Using yield inside a comprehension. From: Chris Angelico To: python-list@python.org 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: 105 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1385476005 news.xs4all.nl 16006 [2001:888:2000:d::a6]:48962 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:60515 On Wed, Nov 27, 2013 at 12:54 AM, Jonathan Slenders wrote: > Where do I find the PEP that describes that the following statement assigns > a generator object to `values`? > values = [ (yield x) for x in range(10) ] > > I assume it's equivalent to the following: > values = (x for x in range(10)) > > > The reason for asking this is that I see no point in using the first syntax > and that the first has another meaning in Python 2.7, if used inside a > function. No, it's not the same; after yielding the ten values, the first one then _returns_ the list. It's a weird and confusing syntax to use for such a thing, but fundamentally it's (more or less) this: def listcomp(): ret = [] for x in range(10): ret.append((yield x)) return ret values = listcomp() Worded like that, you should be able to see what listcomp does. It yields the integers 0 through 9, collects up any values sent to it (or None if you use next()), and returns that collection as a list, which makes it the value of the StopIteration. If you ignore the value associated with StopIteration, then the two are equivalent: >>> values = (x for x in range(10)) >>> list(values) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> values = [ (yield x) for x in range(10) ] >>> list(values) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] But not otherwise: >>> values = (x for x in range(10)) >>> next(values) 0 >>> values.send("asdf") 1 ... >>> values.send("asdf") 9 >>> values.send("asdf") Traceback (most recent call last): File "", line 1, in values.send("asdf") StopIteration >>> values = [ (yield x) for x in range(10) ] >>> next(values) 0 >>> values.send("asdf") 1 ... as above ... >>> values.send("asdf") 9 >>> values.send("asdf") Traceback (most recent call last): File "", line 1, in values.send("asdf") StopIteration: ['asdf', 'asdf', 'asdf', 'asdf', 'asdf', 'asdf', 'asdf', 'asdf', 'asdf', 'asdf'] This is an example of Python doing exactly what it's told, even if you point the gun at your toes and pull the trigger. It's like creating a class in which __add__ prints out the value of the object and __repr__ deletes the third element and returns it. Using a list comprehension to create a generator is technically legal, but outside of the International Obfuscated Python Code Contest, seldom advisable. The difference in 2.7 is (if I've understood correctly) to do with the way a list comp is now part of a function. Others will correct me if I'm wrong, but I believe the equivalent 2.7 code (what I described above was the 3.3 code) would look like this (again, hand-waving some details): _ = [] for x in range(10): _.append((yield x)) values = _ So the yield would take place in the context of the surrounding function, turning _it_ into a generator - and then assigning to values the list of ten send()s, or ten Nones. >>> def foo(): values = [ (yield x) for x in range(10) ] print(values) >>> list(foo()) [None, None, None, None, None, None, None, None, None, None] [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] First line comes from print, second line is what list(foo()) returned. ChrisA