以前就遇到這個問題了: python
#!/usr/bin/python # -*- coding: utf-8 -*- """ Function: 【已解決】Python中如何在嵌套函數內部訪問被嵌套(的父級函數)中的(局部,非全局)變量 http://www.crifan.com/python_access_parent_nesting_function_local_variable_from_nested_function Author: Crifan Li Version: 2012-12-25 Contact: admin at crifan dot com """ def accessVarFromNestedFunc(): localVarInParent = 1; def nestedFunc(): # localVarInParent = localVarInParent + 1 ; print "In nested func, localVarInParent=",localVarInParent; nestedFunc(); print "In current parent nesting func, localVarInParent=",localVarInParent; if __name__ == "__main__": accessVarFromNestedFunc();
即,在嵌套函數內部,操做,父級函數中的變量,可是對應的父級函數中該變量,只是個普通的局部變量。
可是結果會出錯的: 函數
D:\tmp\tmp_dev_root\python\access_var_from_nested_func>access_var_from_nested_func.py Traceback (most recent call last): File "D:\tmp\tmp_dev_root\python\access_var_from_nested_func\access_var_from_nested_func.py", line 24, in <module> accessVarFromNestedFunc(); File "D:\tmp\tmp_dev_root\python\access_var_from_nested_func\access_var_from_nested_func.py", line 20, in accessVarFromNestedFunc nestedFunc(); File "D:\tmp\tmp_dev_root\python\access_var_from_nested_func\access_var_from_nested_func.py", line 17, in nestedFunc localVarInParent = localVarInParent + 1 ; UnboundLocalError: local variable ‘localVarInParent’ referenced before assignment可是一直也嘗試過,寫成global的形式,可是仍是無法解決。
#!/usr/bin/python # -*- coding: utf-8 -*- """ Function: 【已解決】Python中如何在嵌套函數內部訪問被嵌套(的父級函數)中的(局部,非全局)變量 http://www.crifan.com/python_access_parent_nesting_function_local_variable_from_nested_function Author: Crifan Li Version: 2012-12-25 Contact: admin at crifan dot com """ localVarInParent = 1; def accessVarFromNestedFunc(): def nestedFunc(): # global localVarInParent; localVarInParent = localVarInParent + 1 ; print "In nested func, localVarInParent=",localVarInParent; nestedFunc(); print "In current parent nesting func, localVarInParent=",localVarInParent; if __name__ == "__main__": accessVarFromNestedFunc();可是很明顯,不是想要的效果。
#!/usr/bin/python # -*- coding: utf-8 -*- """ Function: 【已解決】Python中如何在嵌套函數內部訪問被嵌套(的父級函數)中的(局部,非全局)變量 http://www.crifan.com/python_access_parent_nesting_function_local_variable_from_nested_function Author: Crifan Li Version: 2012-12-25 Contact: admin at crifan dot com """ def accessVarFromNestedFunc(): localVarInParent = [1]; #here just define a list, first value is what we want to use def nestedFunc(): # localVarInParent[0] = localVarInParent[0] + 1 ; # localVarInParent[0] is the first value of above list value: localVarInParent, and its initial value is 1 print "In nested func, localVarInParent[0]=",localVarInParent[0];#2,3,4,5,6 for i in range(5): nestedFunc(); # here can got value is 6, which is changed after nested function print "In current parent nesting func, localVarInParent[0]=",localVarInParent[0]; #In current parent nesting func, localVarInParent[0]= 6 if __name__ == "__main__": accessVarFromNestedFunc();針對Python 3.x,是能夠添加對應的nonlocal的聲明的。這個暫時不去折騰了,有空再試試。 【總結】 Python中,嵌套函數內部去操做被嵌套的父級函數中的變量的話: Python 2.x:把變量弄進一個列表中的第1個值,index=0,而後就能夠在嵌套函數中,得到該list列表變量,操做其中第1個值了。 Python 3.x:把變量定義爲nonlocal便可。