Tip on the "Late Binding Closures" section

未关闭
#526 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
48/100
Issue 类型
文档
描述清晰度
基本清楚
活跃度
停滞
技术栈
python
领域
documentation

调研方向

阅读“Late Binding Closures”部分,尤其是“What You Should Do Instead”,并将建议的生成器示例与现有解释进行比较。运行所提供的 create_multipliers() 循环,以验证闭包行为。完成的标准是文档准确呈现任何有效的替代方案,并包含一个清晰的支持性示例。

由索引模型根据 Issue 内容生成。

描述

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.

主要语言
Batchfile
星标
29.8k
派生
5.9k
PR 合并指标
30 天内没有已合并 PR

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

realpython/python-guide 的其他 Issue

查看 realpython/python-guide 的全部 Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。