標籤:

python exec in d 是往d中填充數據?

先定義一個空的字典d={}

然後執行一個exec 『a=9』 in d。

這時候d裡面有了globals() 裡面包含的項。這個d是用來存儲exec執行後當前環境下globals 的容器?


先通過文檔把一些概念理順:

6. Simple statements

exec_stmt ::= "exec" or_expr ["in" expression ["," expression]]

This statement supports dynamic execution of Python code. The first expression should evaluate to either a string, an open file object, a code object, or a tuple.

...

In all cases, if the optional parts are omitted, the code is executed in the current scope. If only the first expression after in is specified, it should be a dictionary, which will be used for both the global and the local variables. If two expressions are given, they are used for the global and local variables, respectively. If provided, locals can be any mapping object. Remember that at module level, globals and locals are the same dictionary. If two separate objects are given as globals and locals, the code will be executed as if it were embedded in a class definition.

見下劃線部分,後面倆參數是全局變數和局部變數,如果只加一個,那全局和局部就都在這裡,如果加了倆就分開,如果不加,就在當前scope下運行。

關於code block和scope:

4. Execution model

Names refer to objects. Names are introduced by name binding operations. Each occurrence of a name in the program text refers to the binding of that name established in the innermost function block containing the use.

(名字是對象的引用)

A block is a piece of Python program text that is executed as a unit. The following are blocks: a module, a function body, and a class definition. Each command typed interactively is a block. A script file (a file given as standard input to the interpreter or specified on the interpreter command line the first argument) is a code block. A script command (a command specified on the interpreter command line with the 『-c『 option) is a code block. The file read by the built-in function execfile() is a code block. The string argument passed to the built-in function eval() and to the exec statement is a code block. The expression read and evaluated by the built-in function input() is a code block.

(模塊、函數體、類定義、互動式輸入的每個語句、一個腳本、python -c 方式之行的一條命令、execfile讀入的文件、eval()執行的一條語句、exec執行的一條語句、被input()讀取並求值的一個表達式,都是code block)

A code block is executed in an execution frame. A frame contains some administrative information (used for debugging) and determines where and how execution continues after the code block』s execution has completed.

A scope defines the visibility of a name within a block. If a local variable is defined in a block, its scope includes that block. If the definition occurs in a function block, the scope extends to any blocks contained within the defining one, unless a contained block introduces a different binding for the name. The scope of names defined in a class block is limited to the class block; it does not extend to the code blocks of methods – this includes generator expressions since they are implemented using a function scope. ....

最後的結論是:

  1. exec 所執行的語句,是一個code block

  2. 這個code block 是在一個 execution frame 內執行的,
  3. 可以指定一個字典作為 exec 參數,作為這個code block中變數的scope

  4. 如果不指定,就把 exec 語句所在的scope作為這個code block的scope

沒有『默認填充』一說,而是語句相當於執行在當前的scope下。驗證一下:

a_local_var = 12345
exec "a_local_var = 23456"
print a_local_var # 應該輸出 23456

---補充---

&>&>&> g_d = {}
&>&>&> l_d = {}
&>&>&> global g_var
&>&>&> g_var = 123
&>&>&> globals().keys()
["__builtins__", "__package__", "l_d", "g_d", "__name__", "g_var", "__doc__"]
&>&>&> locals().keys()
["__builtins__", "__package__", "l_d", "g_d", "__name__", "g_var", "__doc__"]
&>&>&> exec "xxx=123" in g_d, l_d
&>&>&> g_d.keys()
["__builtins__"] # 實際上,這裡有的是__builtins__內建函數,只不過裡面東西比較多,列印出來是一大坨,你不要看value,只要看key就清楚了
&>&>&> l_d.keys()
["xxx"]
&>&>&> globals().keys()
["__builtins__", "__package__", "l_d", "g_d", "__name__", "g_var", "__doc__"]
&>&>&> locals().keys()
["__builtins__", "__package__", "l_d", "g_d", "__name__", "g_var", "__doc__"]

&>&>&> d_all = {}
&>&>&> exec "aaa=123" in d_all # 再試試全局跟局部一坨的狀況
&>&>&> d_all.keys()
["__builtins__", "aaa"]


推薦閱讀:

python作為腳本語言和c/c++ 等語言的優勢和劣勢在哪裡地方?python比較成熟用途在哪裡方面?
什麼樣的 Python 編輯器比較適合新手?
計算化學,有python基礎還有必要學習matlab么?
如何看待通過《笨辦法學Python》入門編程的行為?
有關python3的multiprocessing.Pool.map問題,發生錯誤??

TAG:Python |