python基础----1. globals和locals
官方文档
globals
"""
Return a dictionary representing the current global symbol table. This is always the dictionary of the current module (inside a function or method, this is the module where it is defined, not the module from which it is called).
返回表示当前全局符号表的字典。这总是当前模块的字典(在函数或方法中,不是调用它的模块,而是定义它的模块)。
"""
locals
"""
Update and return a dictionary representing the current local symbol table. Free variables are returned by locals() when it is called in function blocks, but not in class blocks. Note that at the module level, locals() and globals() are the same dictionary.
更新并返回表示当前本地符号表的字典。 在函数代码块但不是类代码块中调用 locals() 时将返回自由变量。 请注意在模块层级上,locals() 和 globals() 是同一个字典。
"""
"""
When a name is used in a code block, it is resolved using the nearest enclosing scope. The set of all such scopes visible to a code block is called the block’s environment.
当一个名字在一个代码块中被使用时,将从最近的封闭作用域开始查找此名字……
"""
"""
If a name is bound in a block, it is a local variable of that block, unless declared as nonlocal. If a name is bound at the module level, it is a global variable. (The variables of the module code block are local and global.) If a variable is used in a code block but not defined there, it is a free variable.
如果一个名称被绑定在一个块中,那么它就是该块的一个局部变量,除非声明为非局部变量。如果名称在模块级绑定,则它是一个全局变量。(模块代码块的变量是局部变量和全局变量。)如果一个变量在一个代码块中使用,但没有在那里定义,那么它就是一个自由变量。
"""
举例说明
例1
var1 = 'at the top of module'
print('var1 in locals():', 'var1' in locals()) #
print('var1 in globals():', 'var1' in globals()) # def func():
var2 = 'in the scope of func'
print('va2 in locals():', 'var2' in locals()) #
print('va2 in globals():', 'var2' in globals()) # func()
结果1
var1 in locals(): True
var1 in globals(): True
va2 in locals(): True
va2 in globals(): False
"""
结果说明:
1. 1和2都为True是因为在模块层级上,locals() 和 globals() 是同一个字典
2. 同1
3. var2为函数体内定义的局部变量,所以3为True,4为False
4. 同3
"""
例2
n = 100 def func():
level, n = 'l1', 33
print(locals()) #
print('level' in globals()) # def outer():
print(locals(), n) #
print(locals()) #
print('level' in globals()) #
print(globals()['n']) # outer() func()
结果2
{'level': 'l1', 'n': 33}
False
{'n': 33} 33
{'n': 33}
False
100
"""
首先,在函数体外面只定义了一个全局变量,其余的全是局部变量。
1. 打印locals()时,会打印出函数体内定义的两个局部变量level和n
2. level为局部变量,所以2为False
3. 如果一个局部变量在一个代码中使用,但没有在那里定义,那它就是一个自由变量,自由变量会通过locals()返回,所以locals()中有n
4. outer函数本身没有定义局部变量,而函数内只使用了n这一局部变量,所以locals()也只返回n
5. 此模块只定义了一个全局变量,在模块顶部n=100,所以5为False,6为100
6. 同上
"""
注意:不管是locals()还是globals()的字典,其存储变量的名称时都是以字符串形式存储的。例如
a = 100
# globals()[a] # 会报错
globals()['a'] # 会返回正确的值100
python基础----1. globals和locals的更多相关文章
- Python基础_eval(),exec(),globals(),locals(),compile()
转发:http://www.cnblogs.com/yyds/p/6276746.html 1. eval函数 函数的作用: 计算指定表达式的值.也就是说它要执行的Python代码只能是单个运算表达式 ...
- Python 基础之返回值与函数使用与局部变量和全局变量locals() 和 globals()
一.函数的返回值 return return: 自定义返回值,返回到哪里? 返回到函数的[调用处]1.return 后面可以跟上六个标准数据类型,除此之外,可以跟上 类对象,函数,如果不写return ...
- devi into python 笔记(七)locals与globals 字典格式化字符串 字符集
locals()与globals(): """ locals:局部命名空间 globals:全局命名空间 都是以dictionary的形式保存的,变量名是键,变量值是值 ...
- python globals和locals
文章里面说globals和locals函数返回的是命名空间 - 一个存有对应作用域的所有的变量.方法的字典,注意这里和dir函数返回数组的不一样 Python命名空间的本质 class Test(ob ...
- Python globals()和locals()比较
Python的两个内置函数,globals()和locals() ,它们提供了基于字典的访问局部和全局变量的方式. globals()是可写的,即,可修改该字典中的键值,可新增和删除键值对. 而loc ...
- Python globals和locals函数_reload函数
Python globals和locals函数_reload函数: globals( ): 返回所有能够访问到的全局名字 num = 5 sum = 0 def add(num): func_sum ...
- Python基础学习笔记(十一)函数、模块与包
参考资料: 1. <Python基础教程> 2. http://www.runoob.com/python/python-functions.html 3. http://www.liao ...
- 第四章:Python基础の快速认识內置函数和操作实战
本課主題 內置函数介紹和操作实战 装饰器介紹和操作实战 本周作业 內置函数介紹和操作实战 返回Boolean值的內置函数 all( ): 接受一個可以被迭代的對象,如果函数裡所有為真,才會真:有一個是 ...
- python基础——内置函数
python基础--内置函数 一.内置函数(python3.x) 内置参数详解官方文档: https://docs.python.org/3/library/functions.html?highl ...
随机推荐
- (Python2)自动对话机器人 代码
dict = { 'hello': 'hello'}flag = 'c'work = Trueprint 'hi,my name is python.'print 'do you want chat ...
- MYSQL数据库的设计与调优
优化思路: 1.检查数据表结构,改善不完善设计 2.跑一遍主要业务,收集常用的数据库查询SQL 3.分析查询SQL,适当拆分,添加索引等优化查询 4.优化SQL的同时,优化代码逻辑 5.添加本地缓存和 ...
- c\c++里struct字节对齐规则
规则一.: 每个成员变量在其结构体内的偏移量都是成员变量类型的大小的倍数. 规则二: 如果有嵌套结构体,那么内嵌结构体的第一个成员变量在外结构体中的偏移量,是内嵌结构体中那个数据类型大小最大的成员 ...
- 简单文本悬浮div提示效果
<html> <head> <script src="jquery-1.9.1.min.js" type="text/javascript& ...
- vue.js基础
1,感谢菜鸟教程 2,第一个实例 <html> <head> <meta charset="utf-8"> <title>Vue 测 ...
- vuecli3 项目添加配置文件以及使用@映射、代理
在根目录下新建 vue.config.js 1.vue.config.js中配置路径别名方法 // vue.config.js module.exports = { configureWebpack: ...
- 在windows下安装php redis扩展
我在本地是phpstudy集成环境,但是没有redis扩展,需要自己安装 1.先看清楚自己的php配置,在安装对应的 php_redis.dll 和 php_igbinary.dll php_redi ...
- /usr/bin/ld: warning: libavformat.so.57, needed by /home/camera.so, not found (try using -rpath or -rpath-link)
ffmpeg中,使用libavformt.so.57时,查找不到. 解决方案: 修改ld.so.conf文件,添加路径. sudo gedit /etc/ld.so.conf 在文件末尾添加路径 /u ...
- 适用于typecho0.9的评论表情插件
依旧是寻找插件,实在是太累人,很多插件现在更新后不支持typecho0.9了,今天想给评论框加一个表情拓展,发现新版本的插件完全不兼容typecho0.9,无奈用回旧版本····· 实际上,旧版本的插 ...
- poj 3268 Silver Cow Party(最短路dijkstra)
描述: One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the bi ...