【问题】
之前就遇到这个问题了:
#!/usr/bin/python # -*- coding: utf-8 -*- """ Function: 【已解决】Python中如何在嵌套函数内部访问被嵌套(的父级函数)中的(局部,非全局)变量 https://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 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中如何在嵌套函数内部访问被嵌套(的父级函数)中的(局部,非全局)变量 https://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();
但是很明显,不是想要的效果。
不想要增加全局的变量,只想操作父级函数内的变量。
【解决过程】
1.后来终于看到一个正确的解释了:
How do I change nesting function’s variable in the nested function
针对Python 2.x,则可以改为:
#!/usr/bin/python # -*- coding: utf-8 -*- """ Function: 【已解决】Python中如何在嵌套函数内部访问被嵌套(的父级函数)中的(局部,非全局)变量 https://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即可。