realpython / realpython/python-guide
Tip on the "Late Binding Closures" section
Nobody has claimed this yet.
- Dominant language
- Batchfile
- Stars
- 29.8k
- Forks
- 5.9k
- PR merge metrics
- No merged PRs in 30d
Description
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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Read the "Late Binding Closures" section, especially "What You Should Do Instead," and compare the proposed generator example with the existing explanations. Run the supplied create_multipliers() loop to verify the closure behavior. Done means the documentation accurately presents any valid alternative and includes a clear supporting example.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- documentation
- Issue type
- Documentation
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100