Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #83338

Re: Oddity with lambda and yield

Path csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <ian.g.kelly@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.007
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'python3': 0.07; 'function,': 0.09; 'pep': 0.09; 'python': 0.11; 'def': 0.12; 'bug': 0.12; 'jan': 0.12; '"hello': 0.16; '380': 0.16; 'expression,': 0.16; 'generator.': 0.16; 'lambda': 0.16; 'lambda:': 0.16; 'none]': 0.16; 'sane': 0.16; 'subject:yield': 0.16; 'wrote:': 0.18; 'thu,': 0.19; 'seems': 0.21; '>>>': 0.22; "shouldn't": 0.24; "i've": 0.25; 'equivalent': 0.26; 'header:In- Reply-To:1': 0.27; 'function': 0.29; 'chris': 0.29; 'am,': 0.29; 'dec': 0.30; 'message-id:@mail.gmail.com': 0.30; 'skip:( 20': 0.30; 'code': 0.31; '"",': 0.31; '>>>>': 0.31; 'bug?': 0.31; 'purely': 0.31; 'file': 0.32; 'run': 0.32; 'quite': 0.32; 'linux': 0.33; '(most': 0.33; 'subject:with': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'doubt': 0.36; 'functions.': 0.36; 'yield': 0.36; 'to:addr:python-list': 0.38; 'anything': 0.39; 'recent': 0.39; 'to:addr:python.org': 0.39; 'around.': 0.60; 'introduced': 0.61; 'more': 0.64; 'legal': 0.71; 'discovered': 0.83; '2014,': 0.84; '2015': 0.84; 'this;': 0.91
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=+fPa3SHqbe/osvm49YfjqoZzgQfi1ydfoo82bFYKUws=; b=vxMQj010+fZn85KdW/1kOszoObo9wz3uSAmxIGBVopbCz0Y1WC3C5g1mWFU6Nsr5vt aY8wvQ9yqC8FuTBaOEydt00lk13eutfqKa7uLzC2hBF7aXtxa+uYs63U7tK+44ALiOMx 4eaYS5PiQhI8euHQM7AzWOxI/POzEbvjnH3QmdG/hqG1IMfr53VJ3mf/wrHwuae7NyzH AMEEXiIq17fxE1LIv3k7tO4A/9453lvmaxp/tIHNQFql4bzP/GavQ6lc9ldrwl4c5Lx5 /yegXlgMM1phfJgoQkdaUq3l8y1xo173wnCixvtgUCivGAZtGjID3aKmwodoLuAbRsou k8jg==
X-Received by 10.66.90.161 with SMTP id bx1mr15325253pab.35.1420727453139; Thu, 08 Jan 2015 06:30:53 -0800 (PST)
MIME-Version 1.0
In-Reply-To <CAPTjJmrVpBReySTvnxLgKnS519EoPDR5Y1W4gD851E4j-a4kvQ@mail.gmail.com>
References <CAPTjJmrVpBReySTvnxLgKnS519EoPDR5Y1W4gD851E4j-a4kvQ@mail.gmail.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date Thu, 8 Jan 2015 07:30:11 -0700
Subject Re: Oddity with lambda and yield
To Python <python-list@python.org>
Content-Type text/plain; charset=UTF-8
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.17474.1420727455.18130.python-list@python.org> (permalink)
Lines 46
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1420727455 news.xs4all.nl 2934 [2001:888:2000:d::a6]:52509
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:83338

Show key headers only | View raw


On Thu, Jan 8, 2015 at 5:11 AM, Chris Angelico <rosuav@gmail.com> wrote:
> As yield is an expression, it's legal in a lambda function, which then
> means you have a generator function. But it's not quite the same as
> the equivalent function made with def:
>
> $ python3
> Python 3.5.0a0 (default:1c51f1650c42+, Dec 29 2014, 02:29:06)
> [GCC 4.7.2] on linux
> Type "help", "copyright", "credits" or "license" for more information.
>>>> f=lambda: (yield 5)
>>>> x=f()
>>>> next(x)
> 5
>>>> x.send(123)
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> StopIteration
>>>> def f(): return (yield 5)
> ...
>>>> x=f()
>>>> next(x)
> 5
>>>> x.send(123)
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> StopIteration: 123
>
>
> Is this a bug? I very much doubt any sane code will ever run into
> this; I discovered this purely by chance, after noting that
> Python/compile.c had code to create a generator. The same thing
> happens with Python 3.4.2 on Debian Jessie, so this isn't a bug I've
> introduced in my local fiddling around.

I don't see anything in PEP 380 suggesting that it shouldn't apply to
lambda functions.

>>> def f(g):
...     v = (yield from g)
...     yield v
...
>>> g = lambda: (yield 42) or (yield 43) or (yield 44) or "hello world"
>>> list(f(g()))
[42, 43, 44, None]

Seems like a bug to me.

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Oddity with lambda and yield Ian Kelly <ian.g.kelly@gmail.com> - 2015-01-08 07:30 -0700

csiph-web