【ZZ已解决】Python中如何在嵌套函数内部访问被嵌套(的父级函数)中的(局部,非全局)变量


以前就遇到这个问题了: 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();
可是很明显,不是想要的效果。

不想要增长全局的变量,只想操做父级函数内的变量。





【解决过程】

1.后来终于看到一个正确的解释了:

How do I change nesting function’s variable in the nested function

针对Python 2.x,则能够改成:

#!/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便可。
相关文章
相关标签/搜索