Tutorial on defining functions has potentially misleading explanation of closure
還沒有人認領這個 Issue。
- 主要語言
- Python
- 星號
- 77.2k
- 分支
- 35.9k
- PR 合併指標
- PR 指標待擷取
描述
Documentation
Apologies if I'm using the term closure wrong, I come from more of a Scheme background.
The tutorial says that variable references look "in the local symbol tables of enclosing functions". However, it is not obvious that this refers to functions enclosing the definition, rather than functions enclosing a call to the function. When I first read it I was somewhat confused as it seemed to contradict what I knew about closure in Python.
The following demonstrates half of this:
>>> def f():
... print(n)
...
>>> n = 1
>>> f()
1
>>> def g():
... n = 2
... f()
...
>>> g()
1
>>>
If the variable reference looked in the executing function's local symbol table, we would expect that the last call to g would print 2, rather than 1. However, it does not look in the symbol table of the function enclosing its call, going straight to the global symbol table.
This demonstrates the other half, where the value is obtained from the local symbol table of the function enclosing the call:
>>> def f():
... def g():
... print(n)
... n = 1
... g()
... n = 2
... g()
...
>>> f()
1
2
>>>
In my opinion all that is required is a change in wording, perhaps to "functions enclosing the definition".
Linked PRs
- gh-151038
貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
研究方向
開啟 docs.python.org/3/tutorial/controlflow.html#defining-functions 中的教學區段,檢查關於外圍函式區域符號表的措辭。在進行任何變更之前檢查連結的 PR gh-151038;完成的標準是說明明確指向包含定義的函式,而不是呼叫。
由索引模型根據 Issue 內容生成。
評估
- 技術堆疊
- python
- 領域
- documentation
- Issue 類型
- 文件
- 難度
- 1/5
- 預估耗時
- 1 小時以內
- 活躍度
- 停滯
- 描述清晰度
- 描述清楚
- 新手友好度
- 30/100