New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Generator mangles returned lists. #40392
Comments
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? |
Logged In: YES Sorry, this isn't a bug. You've created a cute example of For some fun, post this on comp.lang.python and expect some Essentially the issue is that that the first yield is When 'l' is re-assigned with " l = l[-1:]", the original If none of this is clear to you, try wrapping the output [id(i) for i in gen()] |
Logged In: YES Ah. I get it. I guess it's time to pull out copy.copy(). |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: