On Sat, Feb 22, 2020 at 10:32 AM Greg Ewing <greg.ew...@canterbury.ac.nz> wrote:
>
> On 22/02/20 11:45 am, Andrew Barnert via Python-ideas wrote:
> > there’s no reason you can’t write `[(yield None) for _ in range(3)]` to 
> > gather the first three values sent into your generator
>
> Currently this doesn't quite do what you might expect.
> It doesn't make the enclosing function into a generator,
> it make the list comprehension itself a generator:
>
>  >>> def f():
> ...  return [(yield x) for x in range(10)]
> ...
>  >>> g = f()
>  >>> g
> <generator object f.<locals>.<listcomp> at 0x6b396c>
>  >>>
>
> I don't think this behaviour is deliberate; it seems to
> be a consequence of deciding to compile the body of the
> comprehension as a nested function.
>

Depends what you mean by "currently".

Python 3.7.0a4+ (heads/master:95e4d58913, Jan 27 2018, 06:21:05)
[GCC 6.3.0 20170516] on linux
>>> def f():
...     return [(yield x) for x in range(10)]
...
>>>

Python 3.8.2rc2+ (heads/3.8:a207512121, Feb 21 2020, 21:49:46)
[GCC 6.3.0 20170516] on linux
>>> def f():
...     return [(yield x) for x in range(10)]
...
  File "<stdin>", line 2
SyntaxError: 'yield' inside list comprehension

https://docs.python.org/3/whatsnew/3.8.html#changes-in-python-behavior

ChrisA
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/XGAZFCMKUD2VCECYVBPKYDHMUUUBM3O3/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to