python locals

Python locals() function returns a dictionary representing the current local symbol table.

Python locals()函数返回一个表示当前本地符号表的字典 。

Python program maintains program information in symbol tables. There are two types of symbol tables:

Python程序在符号表中维护程序信息。 符号表有两种类型:

  • Local Symbol Table – stores information related to the local scope of the program. We can get this detail using locals() function.本地符号表–存储与程序的本地范围有关的信息。 我们可以使用locals()函数获得此详细信息。
  • Global Symbol Table – stores information related to the global scope of the program. We can get this detail using globals() function.全局符号表–存储与程序的全局范围有关的信息。 我们可以使用globals()函数获得此详细信息。

Python symbol table contains details about variable names, methods, classes, etc.

Python符号表包含有关变量名称,方法,类等的详细信息。

Python locals() (Python locals())

Python locals() function doesn’t take any argument. Let’s see the dictionary returned by locals() function.

Python locals()函数不带任何参数。 让我们看看locals()函数返回的字典。

print(locals())

Output:

输出:

{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x10ab79358>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': '/Users/pankaj/Documents/github/journaldev/Python-3/basic_examples/python_locals_example.py', '__cached__': None}

If you will execute print(globals()), you will get the same output. However, the output might vary a little based on your Python installation.

如果执行print(globals()) ,将得到相同的输出。 但是,根据您的Python安装,输出可能会有所不同。

So where is the difference between locals() and globals()?

那么locals()和globals()之间的区别在哪里?

There is no difference because we are executing locals() and globals() in the current module itself. The difference will be present when we call these functions inside a method or class.

这没有什么区别,因为我们在当前模块本身中执行locals()和globals()。 当我们在方法或类中调用这些函数时,会出现差异。

内部方法的Python locals() (Python locals() inside method)

Let’s see what is the output when locals() and globals() are invoked inside a function body.

让我们看看在函数体内调用locals()和globals()时的输出是什么。

# locals() inside function
def fx1():var = 1global glgl = 'x'print('\nlocals() value inside function\n', locals())print('\nglobals() value inside function\n', globals())fx1()

Output:

输出:

locals() value inside function{'var': 1}globals() value inside function{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x10277c358>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': '/Users/pankaj/Documents/github/journaldev/Python-3/basic_examples/python_locals_example.py', '__cached__': None, 'fx1': <function fx1 at 0x1027141e0>, 'gl': 'x'}

So it’s clear that locals() inside function returns the local variable, notice that global variables are part of global symbol table dictionary.

因此很明显,函数内部的locals()返回了局部变量,请注意,全局变量是全局符号表字典的一部分。

类内部的Python locals() (Python locals() inside class)

Let’s see the output when locals() is called inside class body.

让我们看看在类体内部调用locals()时的输出。

# locals() inside class
class Data:x = 0print('\nlocals() value inside class\n', locals())print('\nglobals() value inside class\n', globals())

Output:

输出:

locals() value inside class{'__module__': '__main__', '__qualname__': 'Data', 'x': 0}globals() value inside class{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x10277c358>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': '/Users/pankaj/Documents/github/journaldev/Python-3/basic_examples/python_locals_example.py', '__cached__': None, 'fx1': <function fx1 at 0x1027141e0>, 'gl': 'x'}

When invoked inside the class body, locals() contains the module name, class name and class variables.

在类主体中调用时,locals()包含模块名称,类名称和类变量。

结论 (Conclusion)

Python locals() function is mostly used for debugging purpose. We can check what variables are present in the local scope and their values. We can also alter their values since it’s a dictionary, although not recommended.

Python locals()函数主要用于调试目的。 我们可以检查本地范围内存在哪些变量及其值。 由于它是字典,因此我们也可以更改其值,尽管不建议这样做。

GitHub Repository.GitHub存储库中检出完整的python脚本和更多Python示例。

Reference: Official Documentation

参考: 官方文档

翻译自: https://www.journaldev.com/22946/python-locals

python locals

python locals_Python locals()相关推荐

  1. Python中的“ @”(@)符号有什么作用?

    我正在看一些使用@符号的Python代码,但我不知道它的作用. 我也不知道要搜索什么,因为搜索Python文档时会出现,或者当包含@符号时Google不会返回相关结果. #1楼 此代码段: def d ...

  2. 如何使用Python numpy.where()方法

    In Python, we can use the numpy.where() function to select elements from a numpy array, based on a c ...

  3. Python字符串title()

    Python String title() Python字符串title() Python String title() function returns a title cased version ...

  4. Python字符串isdecimal()

    Python String isdecimal() function returns True if all the characters in the string are decimal char ...

  5. Python字符串isalnum()

    Python string isalnum() function returns True if it's made of alphanumeric characters only. A charac ...

  6. Python字符串count()

    Python String count() function returns the number of occurrences of a substring in the given string. ...

  7. Python字符串splitlines()

    Python String splitlines() Python字符串splitlines() Python String splitlines() function returns the lis ...

  8. Python字符串join()方法

    Python string join() method creates a string from an iterable. It joins all the iterable elements wi ...

  9. Python字符串index()

    Python String index() function returns the lowest index where the specified substring is found. If t ...

最新文章

  1. Angular js 具体应用(一)
  2. PHP 修改memory_limit方法
  3. 《R数据可视化手册》一1.4 从Excel文件中加载数据
  4. Linux下安装配置JDK
  5. 信息学奥赛一本通 1004:字符三角形 | OpenJudge NOI 1.1 08
  6. c++ 构造函数数组_“动态数组”的设计与实现
  7. lvs,haproxy实现负载均衡
  8. python装饰器property_python装饰器: @property
  9. iPhone 居然能当公交卡刷了?!
  10. Parquet文件格式简介
  11. WPF+prism框架实战源码和展示
  12. 医药领域知识图谱快速及医药问答项目
  13. 高斯消去法,列主元法,LU分解法python程序
  14. mysql主从配置文件
  15. Android第三方系统有哪些,第三方安卓定制系统LineageOS 14.1支持6款新设备:包括一加3T...
  16. Ubuntu14.04 64位+Python3.4环境下安装matplotlib的方法
  17. even parity
  18. 数据挖掘软件SPSS Clementine 12安装教程
  19. psm倾向得分匹配法举例_倾向得分匹配法 PSM
  20. 批量调整WPS中插入的图片大小

热门文章

  1. win7安装证书时无响应的解决办法
  2. [转载] Python字符串解析
  3. Scrapy中的Spider
  4. exchange EWS 开发随笔二
  5. BerkeleyDB环境API
  6. (三)Omniglot Dataset介绍
  7. 自动驾驶算法-滤波器系列(五)——高级运动模型在UKF中的应用
  8. Hexo中next主题的个性化配置
  9. 冈萨雷斯图像处理Matlab函数汇总
  10. 计算机二级科目有ps吗,计算机二级有ps吗