realpython / realpython/python-guide

Tip on the "Late Binding Closures" section

未關閉
#526 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

主要語言
Batchfile
星號
29.8k
分支
5.9k
PR 合併指標
30 天內沒有已合併 PR

描述

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.

貢獻指南

開啟貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

研究方向

閱讀「Late Binding Closures」章節,尤其是「What You Should Do Instead」,並將提議的產生器範例與現有說明進行比較。執行提供的 create_multipliers() 迴圈,以驗證閉包行為。完成的標準是文件準確呈現任何有效的替代方案,並包含一個清楚的支援範例。

由索引模型根據 Issue 內容生成。

評估

技術堆疊
python
領域
documentation
Issue 類型
文件
難度
2/5
預估耗時
1-3 小時
活躍度
停滯
描述清晰度
基本清楚
新手友好度
48/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。