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


Groups > comp.lang.python > #83338 > unrolled thread

Re: Oddity with lambda and yield

Started byIan Kelly <ian.g.kelly@gmail.com>
First post2015-01-08 07:30 -0700
Last post2015-01-08 07:30 -0700
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

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

#83338 — Re: Oddity with lambda and yield

FromIan Kelly <ian.g.kelly@gmail.com>
Date2015-01-08 07:30 -0700
SubjectRe: Oddity with lambda and yield
Message-ID<mailman.17474.1420727455.18130.python-list@python.org>
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.

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web