[New-bugs-announce] [issue3267] yield in list comprehensions possibly broken in 3.0
Erick Tryzelaar
report at bugs.python.org
Thu Jul 3 08:28:19 CEST 2008
New submission from Erick Tryzelaar <idadesub at users.sourceforge.net>:
This may be a known consequence of python 3.0, but I couldn't find any
reference to it, nor a test case that covers it. Here's a valid use of yield
in 2.5.1:
>>> def foo():
... x=[(yield x) for x in 1,2,3]
... yield 5
... yield x
>>> x=foo()
>>> x.next()
1
>>> x.send(6)
2
>>> x.send(7)
3
>>> x.send(8)
5
>>> x.send(9)
[6, 7, 8]
>>> x.send(10)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration
But in python 3.0, this code results in:
>>> def foo():
... x=[(yield x) for x in (1,2,3)]
... yield 5
... yield x
>>> x=foo()
>>> next(x)
5
>>> x.send(6)
<generator object <listcomp> at 0x3678f0>
>>> x.send(7)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration
Looking further, it seems that this is a comprehension:
>>> def foo(): [(yield 5)]
>>> type(foo())
<class 'generator'>
Whereas this is not:
>>> def foo(): [(yield 5) for x in range(3)]
>>> type(foo())
<class 'NoneType'>
Is this expected behavior?
----------
components: Interpreter Core
messages: 69168
nosy: erickt
severity: normal
status: open
title: yield in list comprehensions possibly broken in 3.0
versions: Python 3.0
_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue3267>
_______________________________________
More information about the New-bugs-announce
mailing list