nonlocal
来源:互联网 发布:sybase恢复数据库 编辑:程序博客网 时间:2024/06/01 16:49
在 Python 中,内层函数对外层作用域中的变量仅有只读访问权限!
而 nonlocal 可以使我们自由地操作外层作用域中的变量!
# 1. 内层函数"可以使用"外层作用域中的变量
def outside():
msg = 'outside'
def inside():
print(msg)
inside()
print(msg)
>>> outside()
outside
outside
# 2. 内层函数"无法修改"外层变量
def outside():
msg = 'outside'
def inside():
# 无法操作外层变量,这里实际上新建了一个内层局部变量罢了~
msg = 'inside'
print(msg)
inside()
print(msg)
>>> outside()
inside
outside
# 3. 使用 nonlocal 修改外层变量
def outside():
msg = 'outside!'
def inside():
nonlocal msg
msg = 'inside!'
print(msg)
inside()
print(msg)
>>> outside()
inside
inside
阅读全文
0 0
- nonlocal
- nonlocal
- nonlocal
- Nonlocal Jumps
- Python3 nonlocal
- python global和nonlocal
- nonlocal与嵌套函数
- python global和nonlocal
- global和nonlocal
- Python-keyword-global & nonlocal
- python nonlocal关键字
- global 和 nonlocal关键字
- python nonlocal语句
- [python]global与nonlocal关键字
- global \ nonlocal 和 闭包
- Chapter 8 Exceptional Control Flow -- Nonlocal Jumps
- Python3.X增加的关键字nonlocal
- python语法32[global与nonlocal比较]
- 编译原理课设之简单编译器实现
- 反射简单使用
- 【模板】nlogn的LIS
- Eclipse 安装Activiti 插件失败解决方法(总结)
- 万能头文件#include <bits/stdc++.h>
- nonlocal
- php 获取客户端的真实IP地址 和 检查客户端从什么地方过来的请求
- Android WiFi开发教程(一)——WiFi热点的创建与关闭
- Linux软件安装总结
- Single Shot MultiBox Detector论文翻译——中英文对照
- js 获取焦点事件所在的对象
- 在windows下通过Anaconda搭建tensorflow环境
- 浅析被element.style所覆盖的样式
- table td 文字显示两行,并且以…结尾,在360兼容模式不兼容解决方案。