Same arguments and more similar documentation for exec() and for eval().
還沒有人認領這個 Issue。
- 主要語言
- Python
- 星號
- 77.2k
- 分支
- 36k
- PR 合併指標
- PR 指標待擷取
描述
Feature or enhancement
This involves changes to the documentation and to the implementation.
I notice that the arguments for exec() and eval() differ in certain ways, which I believe are unnecessary.
When the source is a code object, both calls result in a call to PyEval_EvalCode or, if closure is given, to PyEval_EvalCodeEx.
When the source is a string, there is no closure argument allowed, and the source is compiled as a file input or as an eval input, and then there is a call to PyRun_String or PyRun_StringFlags.
Therefore, the differences between the two functions are solely in the builtin_exec_impl() and builtin_eval_impl() functions in bltinmodule.c., and builtin_exec() and builtin_eval() in bltinmodule.c.h.
- eval() doesn't take a closure argument. It would be useful if calling eval('x := 3'), for example, if x is a nonlocal variable.
- eval() and exec() both require that a globals argument be an actual dict object or subclass. However, the documentation for exec() is incorrect and should be changed, by removing the phrase ' (and not a subclass of dictionary)'.
- When the first argument is a string, eval() returns the result of compiling and running the string as an expresion, and exec() compiles and runs the string as a file input and returns None.
Pitch
I've had difficulty understanding how both of these functions work, due to confusing documentation of them. They largely say the same thing but in different ways, and this obscures the fact that the two functions are mostly the same.
I imagine that the closure argument was not added to eval() as an oversight. However, eval() could be used to access a nonlocal variable in the caller's namespace in the same way as exec() could. It could even modify the variable if the code object is compiled from an expression containing a walrus.
Previous discussion
discussion.
This discussion post has more details and suggestions for making the proposed changes.
貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
研究方向
從 bltinmodule.c 和 bltinmodule.c.h 開始,尤其是 builtin_exec_impl()、builtin_eval_impl()、builtin_exec() 和 builtin_eval()。在決定引數和閉包的預期行為之前,先閱讀連結的 Python 討論,然後檢視 exec() 和 eval() 的文件。當實作和文件一致地描述並支援已達成共識的行為時,即表示完成。
由索引模型根據 Issue 內容生成。
評估
- 技術堆疊
- python
- 領域
- backend, documentation
- Issue 類型
- 功能
- 難度
- 5/5
- 預估耗時
- 一週以上
- 活躍度
- 停滯
- 描述清晰度
- 基本清楚
- 新手友好度
- 35/100