博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【ZZ已解决】Python中如何在嵌套函数内部访问被嵌套(的父级函数)中的(局部,非全局)变量...
阅读量:7193 次
发布时间:2019-06-29

本文共 3463 字,大约阅读时间需要 11 分钟。

hot3.png

之前就遇到这个问题了:

#!/usr/bin/python# -*- coding: utf-8 -*-"""Function:【已解决】Python中如何在嵌套函数内部访问被嵌套(的父级函数)中的(局部,非全局)变量 http://www.crifan.com/python_access_parent_nesting_function_local_variable_from_nested_function Author:     Crifan LiVersion:    2012-12-25Contact:    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.pyTraceback (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 
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 LiVersion:    2012-12-25Contact:    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 LiVersion:    2012-12-25Contact:    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即可。

转载于:https://my.oschina.net/u/1178546/blog/164224

你可能感兴趣的文章
通过jquery 获取文本框的聚焦和失焦方法
查看>>
7 JavaScript Basics Many Developers Aren't Using (Properly)【转】
查看>>
Eclipse之JSP页面的使用
查看>>
Python入门篇-函数、参数及参数解构
查看>>
Android上获取本机安装的应用程序
查看>>
Android 手势识别
查看>>
[SVN(ubuntu)] ubuntu使用svn
查看>>
充电-ios(未完更新中...
查看>>
git功能速查
查看>>
(转载) Java子类与父类之间的对象转换
查看>>
qhfl-4 注册-登录-认证
查看>>
GDAL 地图切片层级计算公式
查看>>
Oracle PL/SQL Developer 上传下载Excel
查看>>
Docker简介
查看>>
无法打开备份设备,出现操作系统错误5
查看>>
CLH队列锁
查看>>
6月14日云栖精选夜读:阿里云将新增印度和印尼数据中心 加速全球化布局
查看>>
Django tutorial part4
查看>>
130242014049+魏俊斌+第3次试验
查看>>
树链剖分
查看>>