Python内置函数(39)——locals
英文文档:
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.
说明:
1. 函数功能返回当前作用域内的局部变量和其值组成的字典,与globals函数类似(返回全局变量)
>>> locals()
{'__package__': None, '__loader__': <class '_frozen_importlib.BuiltinImporter'>, '__doc__': None, '__name__': '__main__', '__builtins__': <module 'builtins' (built-in)>, '__spec__': None} >>> a = 1 >>> locals() # 多了一个key为a值为1的项
{'__package__': None, '__loader__': <class '_frozen_importlib.BuiltinImporter'>, 'a': 1, '__doc__': None, '__name__': '__main__', '__builtins__': <module 'builtins' (built-in)>, '__spec__': None}
2. 可用于函数内。
>>> def f():
print('before define a ')
print(locals()) #作用域内无变量
a = 1
print('after define a')
print(locals()) #作用域内有一个a变量,值为1 >>> f
<function f at 0x03D40588>
>>> f()
before define a
{}
after define a
{'a': 1}
3. 返回的字典集合不能修改。
>>> def f():
print('before define a ')
print(locals()) # 作用域内无变量
a = 1
print('after define a')
print(locals()) # 作用域内有一个a变量,值为1
b = locals()
print('b["a"]: ',b['a'])
b['a'] = 2 # 修改b['a']值
print('change locals value')
print('b["a"]: ',b['a'])
print('a is ',a) # a的值未变 >>> f()
before define a
{}
after define a
{'a': 1}
b["a"]: 1
change locals value
b["a"]: 2
a is 1
>>>
Python内置函数(39)——locals的更多相关文章
- Python内置函数(39)——help
英文文档: help([object]) Invoke the built-in help system. (This function is intended for interactive use ...
- Python内置函数(56)——locals
英文文档: locals() Update and return a dictionary representing the current local symbol table. Free var ...
- 【转】Python 内置函数 locals() 和globals()
Python 内置函数 locals() 和globals() 转自: https://blog.csdn.net/sxingming/article/details/52061630 1>这两 ...
- Python内置函数和内置常量
Python内置函数 1.abs(x) 返回一个数的绝对值.实参可以是整数或浮点数.如果实参是一个复数,返回它的模. 2.all(iterable) 如果 iterable 的所有元素为真(或迭代器为 ...
- Python | 内置函数(BIF)
Python内置函数 | V3.9.1 | 共计155个 还没学完, 还没记录完, 不知道自己能不能坚持记录下去 1.ArithmeticError 2.AssertionError 3.Attrib ...
- python 内置函数和函数装饰器
python内置函数 1.数学相关 abs(x) 取x绝对值 divmode(x,y) 取x除以y的商和余数,常用做分页,返回商和余数组成一个元组 pow(x,y[,z]) 取x的y次方 ,等同于x ...
- Python 内置函数笔记
其中有几个方法没怎么用过, 所以没整理到 Python内置函数 abs(a) 返回a的绝对值.该参数可以是整数或浮点数.如果参数是一个复数,则返回其大小 all(a) 如果元组.列表里面的所有元素都非 ...
- 【转】python 内置函数总结(大部分)
[转]python 内置函数总结(大部分) python 内置函数大讲堂 python全栈开发,内置函数 1. 内置函数 python的内置函数截止到python版本3.6.2,现在python一共为 ...
- python内置函数,匿名函数
一.匿名函数 匿名函数:为了解决那些功能很简单的需求而设计的一句话函数 def calc(n): return n**n print(calc(10)) #换成匿名函数 calc = lambda n ...
随机推荐
- linux上部署JMeter
export JAVA_HOME=/opt/jdk1.8.0_171 export PATH=$PATH:$JAVA_HOME/bin 让环境变量生效 vi /etc/profile 添加下述两行: ...
- Django----将列表按照一定的顺序展示
1.要求:按照文章的时间降序排列,并且只展示前5篇文章 2.需要用到:list的切片知识 ###改造view.py中的视图方法 #列表页 def get_article(request): artic ...
- 腾讯云服务器SMTP ERROR: Failed to connect to server
一般邮件发送失败是 1.配置的问题. 2.扩展问题socket/ssl 百度搜出来一般都是以上的解决办法, 但是我这次遇到的不是. 本地可以放到腾讯云服务器就不行了,扩展也都开了. 后来发现是安全组端 ...
- Linux-硬件
1.服务器 计算节点服务器-用于后台逻辑运算,所以cpu,磁盘读写性能要求较高 web服务器-用于用户请求访问一些页面,如果高并发,磁盘读写性能要好,可以使用raid0或raid1或raid5技术(r ...
- vue笔记-列表渲染
用v-for把一个数组对应为一组元素 使用方法:v-for="(item,index) in items"//也可以使用of替代in { items:源数组 item:数组元素迭代 ...
- python中for嵌套打印图形
# 打印出九九乘法表 1 * 1 = 1 2 * 1 = 2 2 * 2 = 4 3 * 1 = 3 3 * 2 = 6 3 * 3 = 9 4 * 1 = 4 4 * 2 = 8 4 * 3 = 1 ...
- Unity 图形处理(切分与拉伸)
素材的导入设置 1.导入的图片要设置为 Sprite 才能作为UI使用 2.如果需要进行切分,Sprite Mode 选择 Multiple 进行切分和拉伸设置 1.点击进入精灵编辑视图 2.点击按钮 ...
- window.open在ajax里 被浏览器拦截
setLine(row){ let newTab= window.open('about:blank'); this.api.isPrivilege(localStorage.getItem(&quo ...
- String StringBuffer StringBulider 详细看https://www.cnblogs.com/su-feng
主要区别:运行速度和线程安全 StringBuilder > StringBuffer > String String最慢是因为字符串常量不可改变,例如 str +“cccc” 如果 ...
- 两种方法上传本地文件到github(转)
自从使用github以来,一直都是在github网站在线上传文件到仓库中,但是有时因为网络或者电脑的原因上传失败.最重要的原因是我习惯本地编辑,完成以后再一起上传github.看过了几个教程,总结出最 ...