摘要: 在这篇文章中介绍了“Python的闭包与nonlocal”的用法,因为nonlocal是在Python3.0中新增的关键字,python2.x不提供支持,文中提出在Python2.x解决嵌套函数引用外部变量的方法只有使用global 关键字定义全局变量,另一种可行的解决方案是使用列表或字典代替要操作的关键字。示例如下:1.python3.0 使用nonlocal关键字>>> def outer(): x = 1 def inner(): nonlocal x x = 2 print("inner:", x) inner() print("o... 阅读全文
posted @ 2013-04-03 13:55 刘波coding 阅读(5426) 评论(0) 推荐(0) 编辑