[ python-Bugs-971962 ] Generator mangles returned lists.
SourceForge.net
noreply at sourceforge.net
Sun Jun 13 00:58:13 EDT 2004
Bugs item #971962, was opened at 2004-06-13 03:56
Message generated for change (Comment added) made by edw
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=971962&group_id=5470
Category: Python Interpreter Core
Group: Python 2.3
Status: Closed
Resolution: Invalid
Priority: 5
Submitted By: Ed Watkeys (edw)
Assigned to: Nobody/Anonymous (nobody)
Summary: Generator mangles returned lists.
Initial Comment:
I have run into what seems like a bug. Check this out...
def gen():
l = []
l.append('eggs')
l = l[-1:]
yield l
l.append('ham')
l = l[-1:]
yield l
>>> [i for i in gen()]
[['eggs', 'ham'], ['ham']]
>>> g = gen(); [g.next(), g.next()]
[['eggs', 'ham'], ['ham']]
>>> g = gen(); g.next(); g.next()
['eggs']
['ham']
>>> g = gen(); i = g.next(); j = g.next(); [i,j]
[['eggs', 'ham'], ['ham']]
>>> g = gen(); [g.next()[:], g.next()[:]]
[['eggs'], ['ham']]
Anyone have any insight into this?
----------------------------------------------------------------------
>Comment By: Ed Watkeys (edw)
Date: 2004-06-13 04:58
Message:
Logged In: YES
user_id=44209
Ah. I get it. I guess it's time to pull out copy.copy().
----------------------------------------------------------------------
Comment By: Raymond Hettinger (rhettinger)
Date: 2004-06-13 04:20
Message:
Logged In: YES
user_id=80475
Sorry, this isn't a bug. You've created a cute example of
the joys of mutability.
For some fun, post this on comp.lang.python and expect some
lively results.
Essentially the issue is that that the first yield is
returns a mutable list. If printed right away, it will show
its then current value of ['eggs']. Upon restarting the
generator, the list is updated to ['ham', 'eggs'] which is
what prints for the *first* list return value (it is still
the same list with changed contents).
When 'l' is re-assigned with " l = l[-1:]", the original
list is still intact while the value assigned to "l" changes
to be a new list (the slice). So you have the original list
modified and the new list with a different value. Very cute.
If none of this is clear to you, try wrapping the output
with the id() function to see which object is being displayed:
[id(i) for i in gen()]
g=gen(); [id(g.next()), id(g.next())]
----------------------------------------------------------------------
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=971962&group_id=5470
More information about the Python-bugs-list
mailing list