python locals 与 globals 之间有另外一个重要的区别
2015-09-22 11:20
549 查看
locals 与 globals 之间有另外一个重要的区别,您应该在它困扰您之前就了解它。它无论如何都会困扰您的,但至少您还会记得曾经学习过它。
例 8.12. locals 是只读的,globals 不是
<span class="pykeyword"> def</span> foo(arg): x = 1 <span class="pykeyword">print</span> locals() <a target=_blank name="dialect.locals.4.1" style="color: rgb(51, 102, 153);"></a><img border="0" alt="1" src="http://woodpecker.org.cn/diveintopython/images/callouts/1.png" width="12" height="12" style="border: none; max-width: 100%;" /> locals()[<span class="pystring">"x"</span>] = 2 <a target=_blank name="dialect.locals.4.2" style="color: rgb(51, 102, 153);"></a><img border="0" alt="2" src="http://woodpecker.org.cn/diveintopython/images/callouts/2.png" width="12" height="12" style="border: none; max-width: 100%;" /> <span class="pykeyword">print</span> <span class="pystring">"x="</span>,x <a target=_blank name="dialect.locals.4.3" style="color: rgb(51, 102, 153);"></a><img border="0" alt="3" src="http://woodpecker.org.cn/diveintopython/images/callouts/3.png" width="12" height="12" style="border: none; max-width: 100%;" /> z = 7 <span class="pykeyword">print</span> <span class="pystring">"z="</span>,z foo(3) globals()[<span class="pystring">"z"</span>] = 8 <a target=_blank name="dialect.locals.4.4" style="color: rgb(51, 102, 153);"></a><img border="0" alt="4" src="http://woodpecker.org.cn/diveintopython/images/callouts/4.png" width="12" height="12" style="border: none; max-width: 100%;" /><span class="pykeyword"> print</span> <span class="pystring">"z="</span>,z <a target=_blank name="dialect.locals.4.5" style="color: rgb(51, 102, 153);"></a><img border="0" alt="5" src="http://woodpecker.org.cn/diveintopython/images/callouts/5.png" width="12" height="12" style="border: none; max-width: 100%;" />
相关文章推荐
- python 监测主机是否alive
- SyntaxError: Non-ASCII character '\xe5'
- Python包管理工具setuptools详解及entry point
- 用Python读写Excel文件
- [001]Python requests 要点
- Python模块之: configobj
- python访问webserver服务
- python笔记
- Python 元组(tuple)操作
- python 实现 自动 svn checkout
- python3入门之类
- python3入门之类
- How to configure Eclipse for Python programming
- 第一章 安装和运行python
- Python模块之: configobj
- python设计模式 - factory method
- CoffeeScript实现Python装潢器
- learn python in 10 minutes
- Python中的装饰器
- 4.Python进阶_函数参数的对应