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

Tip on the "Late Binding Closures" section #526

Open
borzhang opened this issue Mar 16, 2015 · 0 comments
Open

Tip on the "Late Binding Closures" section #526

borzhang opened this issue Mar 16, 2015 · 0 comments

Comments

@borzhang
Copy link

The two solutions in "What You Should Do Instead" are certainly correct, while there's another solution in that special case, one can simply change

return [lambda x : i * x for i in range(5)]

to

return (lambda x : i * x for i in range(5))

in other words, one can create a generator, rather than a list, to postpone the time when cell contents be evaluated. Actually, a more essential test code could be

for multiplier in create_multipliers():
    print multiplier.__closure__[0].cell_contents

FYI.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant