名字空間
Python 的名字空間是 Python 一個非常核心的內容。
其他語言中如 C 中,變量名是內存地址的別名,而在 Python 中,名字是一個字符串對象,它與他指向的對象構成一個{name:object}關聯。
Python 由很多名字空間,而 LEGB 則是名字空間的一種查找規則。
作用域
Python 中name-object的關聯存儲在不同的作用域中,各個不同的作用域是相互獨立的。而我們就在不同的作用域中搜索name-object。
舉個栗子,來說明作用域是相互獨立的。
In [11]: i = "G"In [12]: def test(): i = "L" print i, "in locals" ....:In [13]: test() L in localsIn [14]: print i, "in globals" G in globals
在上面的栗子中,我們定義了兩次 i,在 test 函數中是 i-L,在外面是 i-G。為什么在 test 函數中,我們 i 指向的是對象 L,而在外面,i 指向的則是 G?這就是 LEGB 的作用。
簡述
簡而言之,LEGB 代表名字查找順序: locals -> enclosing function -> globals -> __builtins__
所以,在 Python 中檢索一個變量的時候,優先回到 locals 里面來檢索,檢索不到的情況下會檢索 enclosing ,enclosing 沒有則到 globals 全局變量里面檢索,最后是到 builtins 里面來檢索。
當然,因為 builtins 的特殊性,我們可以直接在 builtins 里面添加變量,這樣就可以在任意模塊中訪問變量,不過這種方法太過于變態,不推薦這么做。
locals,globals
函數的形參跟內部變量都存儲在 locals 中。
In [1]: def f(x): ...: a = x ...: print a ...: print locals() ...:In [2]: f("hello")hello{'a': 'hello', 'x': 'hello'}不過在函數內部調用global 聲明的時候,可以將變量存儲在 globals 中
In [6]: def f(x): ...: global a ...: a = x ...: print a ...: print locals() ...:In [7]: f("hello")hello{'x': 'hello'}In [8]: print ahelloIn [9]: print x---------------------------------------------------------------------------NameError Traceback (most recent call last)<ipython-input-9-2d264e11d975> in <module>()----> 1 print xNameError: name 'x' is not defined如上面栗子中那樣,在函數中聲明 a 為全局變量,則函數 f 的 locals只有參數 x,而沒有變量,而在外部可以使用變量 a,而使用 x 的時候則是NameError
Enclosed
Enclosing 是外部嵌套函數的名字空間。我們經常在閉包中用到。在 Python3中提供了一個 nonlocal關鍵字來修改外部嵌套函數的名字空間,但是要使用 Python3才有,我等使用 Python2的只能眼饞一下。
In [11]: def outer(): ....: a_var = 'enclosed value' ....: print a_var ....: def inner(): ....: a_var = 'local value' ....: print(a_var) ....: inner() ....: print a_var ....:In [12]: outer()enclosed valuelocal valueenclosed value
下面的栗子簡單示范一下 nonlocal 的用法,實在 Python3下面才可以正常運行的:
In [1]: a_var = 'global value'In [2]: def outer(): ...: a_var = "local value" ...: print("outer befor", a_var) ...: def inner(): ...: nonlocal a_var ...: a_var = "inner value" ...: print("in inner():", a_var) ...: inner() ...: print("outer inner:", a_var) ...:In [3]: outer()outer befor local valuein inner(): inner valueouter inner: inner valueIn [4]: print(a_var)global valuebuiltins
builtins 則是內置模塊,輕易不要修改
In [19]: b---------------------------------------------------------------------------NameError Traceback (most recent call last)<ipython-input-19-3b5d5c371295> in <module>()----> 1 bNameError: name 'b' is not definedIn [20]: __builtins__.b = "builtins"In [21]: bOut[21]: 'builtins'
上面栗子中在第一次調用b的時候報錯NameError,之后我們修改 builtins 的名字空間,將名字b與值"builtins"進行關聯,就可以正常調用了。這種非常規用法不建議使用。
新聞熱點
疑難解答
圖片精選