python finally,python locals_Python locals()

 2023-11-19 阅读 32 评论 0

摘要:python localsPython 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

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 finally? 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())

python index用法?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()?

python const? 那么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()

python locals,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.

python localtime、 讓我們看看在類體內部調用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神奇?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 loc。python locals

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/1/183242.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息