[issue7423] nested generator expression produces strange results

R. David Murray report at bugs.python.org
Wed Dec 2 03:04:02 CET 2009


R. David Murray <rdmurray at bitdance.com> added the comment:

I'd already written then when Benjamin posted his answer, but rather
than waste having written it I'm going to post it anyway :)

You must remember that the purpose of a generator is to evaluate lazily.
 Your expression involving the generator would unwrap this way:

def g1():
    for y in 'c':
        yield x+y
l = []
for x in 'ab':
    l.append(g1())
print l
print map(list, l)

If you run this you will note that 'l' is a pair of generator instances.
 These are not run until the 'map' is executed. By that point the for
loop has completed, and x has its final value, 'b'.  g1 is evaluated
twice, and both times x is 'b', so you get ['bc', 'bc']

----------
nosy: +r.david.murray

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue7423>
_______________________________________


More information about the Python-bugs-list mailing list