[Python-Dev] Generator objects and list comprehensions?
Ivan Levkivskyi
levkivskyi at gmail.com
Wed Jan 25 03:14:27 EST 2017
More information about the Python-Dev mailing list
Wed Jan 25 03:14:27 EST 2017
- Previous message (by thread): [Python-Dev] Generator objects and list comprehensions?
- Next message (by thread): [Python-Dev] Generator objects and list comprehensions?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On 25 January 2017 at 07:01, Chris Angelico <rosuav at gmail.com> wrote: > >>> [(yield 1) for x in range(10)] > > <generator object <listcomp> at 0x10cd210f8> > This is an old bug, see e.g. http://bugs.python.org/issue10544 The ``yield`` inside comprehensions is bound to the auxiliary function. Instead it should be bound to an enclosing function, like it is done for ``await``. The behaviour of ``await`` in comprehensions is intuitive (since it is simply equivalent to a for-loop): >>> async def f(i): ... return i >>> async def g_for(): ... lst = [] ... for i in range(5): ... lst.append(await f(i)) ... print(lst) >>> g_for().send(None) [0, 1, 2, 3, 4] Traceback (most recent call last): File "<stdin>", line 1, in <module> StopIteration >>> async def g_comp(): ... print([await f(i) for i in range(5)]) >>> g_comp().send(None) # exactly the same as g_for [0, 1, 2, 3, 4] Traceback (most recent call last): File "<stdin>", line 1, in <module> StopIteration While current behaviour of ``yield`` in comprehensions is confusing: >>> def c_for(): ... lst = [] ... for i in range(5): ... lst.append((yield i)) ... print(lst) >>> c_for().send(None) 0 >>> c_for().send(None) 1 # etc. >>> def c_comp(): ... print([(yield i) for i in range(5)]) >>> c_comp().send(None) # Naively this should be equivalent to the above, but... <generator object c_comp.<locals>.<listcomp> at 0x7f1fd1faa630> Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'NoneType' object has no attribute 'send' I promised myself to write a patch, but didn't have time for this yet. I hope I will do this at some point soon. -- Ivan -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.python.org/pipermail/python-dev/attachments/20170125/a423f1c6/attachment.html>
- Previous message (by thread): [Python-Dev] Generator objects and list comprehensions?
- Next message (by thread): [Python-Dev] Generator objects and list comprehensions?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-Dev mailing list