python中locals函数_Python locals()函数
# Python `locals()`函数
> 原文: [https://thepythonguru.com/python-builtin-functions/locals/](https://thepythonguru.com/python-builtin-functions/locals/)
* * *
于 2020 年 1 月 7 日更新
* * *
`locals()`函数返回一个字典,其中包含在本地名称空间中定义的变量。 在全局名称空间中调用`locals()`与调用[`globals()`](/python-builtin-functions/globals/)相同,并返回代表模块全局名称空间的字典。
其语法如下:
```py
locals() -> dictionary containg local scope variables
```
这是一个例子:
```py
#!/usr/bin/python3
from pprint import pprint
a = 10
b = 20
def foo():
x = 30 # x and y are local variables
y = 40
print("locals() = {0}".format(locals()))
pprint(locals()) # same as calling globals()
print('*' * 80)
print("locals() == globals()? ", locals() == globals())
print('*' * 80)
foo()
```
**预期输出**:
```py
{'__builtins__': ,
'__cached__': None,
'__doc__': None,
'__file__': 'module1.py',
'__loader__': <_frozen_importlib_external.sourcefileloader object at>,
'__name__': '__main__',
'__package__': None,
'__spec__': None,
'a': 10,
'b': 20,
'foo': ,
'pprint': }
********************************************************************************
locals() == globals()? True
********************************************************************************
locals() = {'y': 40, 'x': 30}
```
试试看:
```py
from pprint import pprint
a = 10
b = 20
def foo():
x = 30 # x and y are local variables
y = 40
print("locals() = {0}".format(locals()))
pprint(locals()) # same as calling globals()
print('*' * 80)
print("locals() == globals()? ", locals() == globals())
print('*' * 80)
foo()
```
* * *
* * *
python中locals函数_Python locals()函数相关推荐
- python中dir用法_Python dir() 函数
Python dir() 函数 描述 dir() 函数不带参数时,返回当前范围内的变量.方法和定义的类型列表:带参数时,返回参数的属性.方法列表.如果参数包含方法__dir__(),该方法将被调用.如 ...
- python中dir用法_Python dir()函数
您可以使用内置的dir()函数列出一个定义对象的标识符.例如,对于一个模块,包括在模块中定义的函数,类和变量. 当你给dir()提供一个模块名字时,它返回在那个模块中定义的名字的列表.当没有为其提供参 ...
- python中eps参数_Python minimize函数:向约束字典传递附加参数
我不知道如何通过minimize函数将其他参数传递给约束字典.我可以成功地将其他参数传递给目标函数. constraints参数是一个dict,它有一个字段"args",其中arg ...
- python中factor函数_Python入门-函数
函数 在维基百科上函数式这样描述的: 函数在数学中为两集合间的一种对应关系:输入值集合中的每项元素皆能对应唯一一项输出值集合中的元素. 此处的函数区别于我们数学上的函数,在编程世界中,函数(Funct ...
- python中实现延时回调普通函数示例代码
这篇文章主要给大家介绍了关于python中实现延时回调普通函数的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧. 回调函 ...
- Python中常用的高阶函数
Python 中常用的高阶函数 ① filter(function,iterable)filter(function, iterable)filter(function,iterable) 过滤器 ...
- Python中的startswith和endswith函数使用实例
Python中的startswith和endswith函数使用实例 在Python中有两个函数分别是startswith()函数与endswith()函数,功能都十分相似,startswith()函数 ...
- python整数转换字符串_使用Python中的str()函数将整数值转换为字符串
python整数转换字符串 Given an integer value and we have to convert the value to the string using str() func ...
- python中zip的使用_浅谈Python中的zip()与*zip()函数详解
前言 1.实验环境: Python 3.6: 2.示例代码地址:下载示例: 3.本文中元素是指列表.元组.字典等集合类数据类型中的下一级项目(可能是单个元素或嵌套列表). zip(*iterables ...
- Python中常用最神秘的函数! lambda 函数深度总结!
今天我们来学习 Python 中的 lambda 函数,并探讨使用它的优点和局限性 Let's do it! 什么是 Python 中的 Lambda 函数 lambda 函数是一个匿名函数(即,没有 ...
最新文章
- sdut 2088 数据结构实验之栈与队列十一:refresh的停车场
- 录音转成文字 android,安卓手机自带的录音转文字功能这么强大,堪称会议神器!你会用吗...
- css如何各种中文字体如雅黑、黑体、宋体、楷体等等
- ASP.NET 2.0运行时简要分析
- 实现.NET Core配置Provider之EF
- selenium常见面试题
- html5开发桌面程序调用dll,使用Visual Studio开发Html5应用
- 第15天android:使用sqlite
- Joseph UVA 1452 Jump
- 实现对span标签的多选单选功能_如何在Notion中做多级标签?-Notion102
- 由PPP项目总结的几点项目经验
- 资本纷纷入局,咖啡赛道还能香多久?
- server sql 将出生日期转为年龄_SQL语句如何根据出生日期计算年龄
- 小白科研笔记:简析SOTA目标检测算法3D-CVF
- 【JavaFx 构建ProAdmin UI界面】
- android build.versioncodes.kitkat,Android 4.1至4.4 KitKat-为API启用TLS 1.2
- oracle 查看时间对应周,oracle数据获取当前自然周,当前周的起始和结束时间
- hand_mysql
- 论文翻译1-PARSEC-Streaming 360-Degree Videos Using Super-Resolution
- WebLogic中间件安装与介绍(详细版约2万字)