我不知道爲什麼引用變量(dict,list)可以從嵌套函數訪問,而簡單對象則不可以。python嵌套函數對外部變量的可訪問性
In [1]: a = 1
In [2]: b = [1]
In [3]: c = {"c" : 1}
In [4]:
In [4]: def f():
...: a = 2
...: b[0] = 2
...: c["c"] = 2
...:
In [5]: f()
In [6]:
In [6]: print a
1
In [7]: print b
[2]
In [8]: print c
{'c': 2}
因爲整數是不可變的,而列表和字典是可變的閱讀http://stackoverflow.com/questions/8056130/immutable-vs-mutable-types-python。 –