Skip to content
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

Closed
edw mannequin opened this issue Jun 13, 2004 · 3 comments
Closed

Generator mangles returned lists. #40392

edw mannequin opened this issue Jun 13, 2004 · 3 comments
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs)

Comments

@edw
Copy link
Mannequin

edw mannequin commented Jun 13, 2004

BPO 971962
Nosy @rhettinger

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:

assignee = None
closed_at = <Date 2004-06-13.04:58:13.000>
created_at = <Date 2004-06-13.03:56:37.000>
labels = ['interpreter-core', 'invalid']
title = 'Generator mangles returned lists.'
updated_at = <Date 2004-06-13.04:58:13.000>
user = 'https://bugs.python.org/edw'

bugs.python.org fields:

activity = <Date 2004-06-13.04:58:13.000>
actor = 'edw'
assignee = 'none'
closed = True
closed_date = None
closer = None
components = ['Interpreter Core']
creation = <Date 2004-06-13.03:56:37.000>
creator = 'edw'
dependencies = []
files = []
hgrepos = []
issue_num = 971962
keywords = []
message_count = 3.0
messages = ['21175', '21176', '21177']
nosy_count = 2.0
nosy_names = ['rhettinger', 'edw']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = None
status = 'closed'
superseder = None
type = None
url = 'https://bugs.python.org/issue971962'
versions = ['Python 2.3']

@edw
Copy link
Mannequin Author

edw mannequin commented Jun 13, 2004

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?

@edw edw mannequin closed this as completed Jun 13, 2004
@edw edw mannequin added invalid interpreter-core (Objects, Python, Grammar, and Parser dirs) labels Jun 13, 2004
@edw edw mannequin closed this as completed Jun 13, 2004
@edw edw mannequin added invalid interpreter-core (Objects, Python, Grammar, and Parser dirs) labels Jun 13, 2004
@rhettinger
Copy link
Contributor

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())]

@edw
Copy link
Mannequin Author

edw mannequin commented Jun 13, 2004

Logged In: YES
user_id=44209

Ah. I get it. I guess it's time to pull out copy.copy().

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs)
Projects
None yet
Development

No branches or pull requests

1 participant