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

Weird behavior with generators with self-referencing output. #57804

Closed
PyryP mannequin opened this issue Dec 13, 2011 · 3 comments
Closed

Weird behavior with generators with self-referencing output. #57804

PyryP mannequin opened this issue Dec 13, 2011 · 3 comments
Labels
type-bug An unexpected behavior, bug, or error

Comments

@PyryP
Copy link
Mannequin

PyryP mannequin commented Dec 13, 2011

BPO 13595
Nosy @amauryfa

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 2011-12-13.22:33:08.072>
created_at = <Date 2011-12-13.19:25:39.576>
labels = ['type-bug']
title = 'Weird behavior with generators with self-referencing output.'
updated_at = <Date 2011-12-14.02:09:43.040>
user = 'https://bugs.python.org/PyryP'

bugs.python.org fields:

activity = <Date 2011-12-14.02:09:43.040>
actor = 'PyryP'
assignee = 'none'
closed = True
closed_date = <Date 2011-12-13.22:33:08.072>
closer = 'amaury.forgeotdarc'
components = []
creation = <Date 2011-12-13.19:25:39.576>
creator = 'PyryP'
dependencies = []
files = []
hgrepos = []
issue_num = 13595
keywords = []
message_count = 3.0
messages = ['149403', '149412', '149416']
nosy_count = 2.0
nosy_names = ['amaury.forgeotdarc', 'PyryP']
pr_nums = []
priority = 'normal'
resolution = 'works for me'
stage = None
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue13595'
versions = ['Python 2.7', 'Python 3.2']

@PyryP
Copy link
Mannequin Author

PyryP mannequin commented Dec 13, 2011

The following self-referencing generator has incorrect output:

def ab_combinations():
    #'', 'a', 'b', 'aa', 'ab', 'ba', 'bb', 'aaa', ...
    def _deferred_output():
        yield ""
        tees = tee(output)

        #This definition works fine: '', 'a', 'b', 'aa', 'ab', 'ba', ...
        l = [(item+"a" for item in tees[0]), (item+"b" for item in tees[1])]

        #This definition results in: '', 'b', 'b', 'bb', 'bb', 'bb', ...
        #l = [(item+label for item in t) for t, label in zip(tees,"ab")]
        
        while True:
            for g in l:
                yield next(g)
result, output = tee(_deferred_output())
return result

@PyryP PyryP mannequin added the type-bug An unexpected behavior, bug, or error label Dec 13, 2011
@amauryfa
Copy link
Member

This is expected, and is due to the late binding of the "label" variable in the "item+label" expression. Look at the example below:

>>> l = [lambda item: item + label for label in "ab"]
>>> f1, f2 = l
>>> print f1(''), f2('')
b b

For the lambda function, 'label' is a free variable, whose value is fetched from the global environment at runtime. But at the time the second time is executed, 'label' has only one value, the last one from the for loop, and both functions only see 'b'.
A Generator Expression also defines a code block, and the late binding also applies.

Now to fix your code, if 'ab' can be of arbitrary length, I can't find a simpler way without yet another inline function:

l = [(lambda lbl:(item + lbl for item in t))(label) for t, label in zip(tees,"ab")]

'lbl' is still a free variable, but now 'lbl' comes from the lambda function, and there is one *different* *function* per iteration of the loop; so they are in fact distinct 'lbl' variables, and the generator expressions will correctly see different labels.

@PyryP
Copy link
Mannequin Author

PyryP mannequin commented Dec 14, 2011

Oh, I was sure it had to do with binding issues. I just couldn't put my finger on it because the behavior seemed so counterintuitive.
Thanks for clearing things up. I can now work around this feature.

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

1 participant