一、變數作用域 當程式定義一個變數時,這個變數是有它的作用範圍的,變數的作用範圍稱為變數的作用域。根據變數的位置,分為兩種: 局部變數:局部變數就是在函數中定義的變數,包括參數,都是局部變數,局部離開函數後,將不能被訪問。 全局變數:不在函數內定義、全局範圍內定義的變數,都是全局變數,全局變數可以在 ...
一、變數作用域
當程式定義一個變數時,這個變數是有它的作用範圍的,變數的作用範圍稱為變數的作用域。根據變數的位置,分為兩種:
- 局部變數:局部變數就是在函數中定義的變數,包括參數,都是局部變數,局部離開函數後,將不能被訪問。
- 全局變數:不在函數內定義、全局範圍內定義的變數,都是全局變數,全局變數可以在所有函數中被訪問。
在Python中,提供了三個工具函數獲取指定範圍內變數和值組成的字典。
- globals():返回當前作用域全局變數的字典;無論在哪裡使用,都會獲取全局變數。
- locals():返回包含當前範圍的局部變數的字典;當在全局範圍內使用,會獲取全局範圍內所有變數組成的字典。
- vars():當沒有參數時,相當於locals();有一個參數時,相當於object.__dict__。
使用globals()和locals()獲取全局變數時,不應該被修改,修改會改變全局變數本身。而locals()獲取局部變數時,即使修改了,也不會對局部變數產生影響。
globals(),例:
a = 1 def test(): b = 2 print (globals())
test() # 列印全局變數 {'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <class '_frozen_importlib.BuiltinImporter'>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, 'a': 1, 'test': <function test at 0x0000000002EAC1E0>} globals() # 列印全局變數 {'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <class '_frozen_importlib.BuiltinImporter'>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, 'a': 1, 'test': <function test at 0x0000000002EAC1E0>}
locals(),例:
a = 1 def test(): b = 2 print (locals()) test() # 列印局部變數 {'b': 2} print (locals()) # 列印全局變數 {'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <class '_frozen_importlib.BuiltinImporter'>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, 'a': 1, 'test': <function test at 0x0000000002EAC1E0>}
vars(),例:
class test01: k1 = 1 def test02(): k2 = 2 print (vars()) k3 = 3 test01.test02() # 列印test02()的局部變數 {'k2': 2} print (vars()) # 列印全局變數 {'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <class '_frozen_importlib.BuiltinImporter'>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, 'test01': <class '__main__.test01'>, 'k3': 3} print (vars(test01)) # 列印類的屬性 {'__module__': '__main__', 'k1': 1, 'test02': <function test01.test02 at 0x00000000023892F0>, '__dict__': <attribute '__dict__' of 'test01' objects>, '__weakref__': <attribute '__weakref__' of 'test01' objects>, '__doc__': None} print (test01.__dict__) # 列印類的屬性 {'__module__': '__main__', 'k1': 1, 'test02': <function test01.test02 at 0x00000000023892F0>, '__dict__': <attribute '__dict__' of 'test01' objects>, '__weakref__': <attribute '__weakref__' of 'test01' objects>, '__doc__': None}
全局變數雖然可以被所有函數訪問,但是如果在函數內定義了與全局變數同名的變數,就會發生局部變數遮蔽全局變數的情況,例:
a = 1 def test(): print (a) test() # 運行成功,列印 1 def test02(): a = 2 print (a) test02() # 函數內部對不存在的變數賦值,會重新定義新的局部變數,列印 2 def test03(): print (a) a = 3 test03() # 報錯UnboundLocalError: local variable 'a' referenced before assignment,由於a=3這段代碼重新定義了局部變數,所以a全局變數被被遮蔽。
二、使用global語句在函數中聲明全局變數
為了避免在函數中對全局變數賦值,可以通過global語句聲明全局變數。
例:
a = 1 def test(): # 聲明a是全局變數,後面的語句將不會重新定義局部變數 global a print (a) # 列印 1 # 對全局變數進行賦值 a = 2 test() # 列印 1 print (a) # 列印 2
三、局部函數
前面我們看到的都是全局函數,我們還可以在函數體內定義函數,這稱為局部函數,局部函數在預設情況下,對外部是隱藏的,只能在其封閉函數內有效,如果想在其他作用域中使用局部函數,其封閉函數可以返回局部函數。
例:
# test()函數根據不同的參數,選擇調用不同的局部函數 def test(x): def a(x): return x * x def b(x): return x if x != 0: return a(x) else: return b(x) print (test(2)) # 列印 4 print (test(0)) # 列印 0
局部函數的變數也會遮蔽他所在函數的局部變數,例:
def test01(): a = 1234 def test02(): print (a) a = 4321 test02() test01() # 報錯 UnboundLocalError: local variable 'a' referenced before assignment
上面的代碼中,由於在test02()函數中重新定義了新的局部變數a,test02()函數中定義的局部變數a遮蔽了他所在函數test01()中的局部變數a,我們可以通過nonlocal語句聲明訪問賦值語句只是訪問該函數所在函數的局部變數。
註意,nonlocal語句只能在嵌套函數中使用,並且在外層函數中必須定義了相關的局部變數,否則會報錯。
例:
def test01(): a = 1234 def test02(): nonlocal a # 聲明a是test01()的局部變數 print ('01',a) a = 4321 # 改變外層函數局部變數的值 print ('02',a) print ('03',a) test02() print ('04',a) test01() # 列印 # 03 1234 # 01 1234 # 02 4321 # 04 4321