当前位置: 首页 > 图灵资讯 > 行业资讯> locals函数在python中使用

locals函数在python中使用

发布时间:2024-10-09 13:58:36

1.说明

locals()该方法用于返回当前的本地符号表字典 ,符号表是编译器维护程序必要信息的数据结构,包括变量、方法、类别等。有两种符号表:

  • 全局符号表

  • 本地符号表

2.语法

locals()

3.参数

4.返回值

返回字典类型的局部变量

5.实例

deflocalsNotPresent():
returnlocals()

deflocalsPresent():
present=True
returnlocals()

print('localsNotPresent:',localsNotPresent())
print('localsPresent:',localsPresent())

运行上述代码时,代码输出为:

localsNotPresent:{}
localsPresent:{'present':True}

注意与globals()不同的方法是locals()不改变本地符号表的信息。

以上是python中locals函数的使用。我们需要关注局部变量。学习后,我们可以尝试我们的实例代码。

(推荐操作系统:windows7系统Python 3.9.1,DELL G3电脑。)

相关文章

如何让vim支持python3

如何让vim支持python3

2025-09-12
python2.7和3.6区别有哪些

python2.7和3.6区别有哪些

2025-09-12
python3有serial库吗

python3有serial库吗

2025-09-12
python中w、r表示什么意思

python中w、r表示什么意思

2025-09-12
python中如何把list变成字符串

python中如何把list变成字符串

2025-09-12
python命名空间是什么

python命名空间是什么

2025-09-12