模块的私有符号表和函数的全局符号选项卡

2024-04-28 22:40:59 发布

您现在位置:Python中文网/ 问答频道 /正文

阅读时 6. Modules,我遇到了

Each module has its own private symbol table, which is used as the global symbol table by all functions defined in the module. Thus, the author of a module can use global variables in the module without worrying about accidental clashes with a user’s global variables. On the other hand, if you know what you are doing you can touch a module’s global variables with the same notation used to refer to its functions, modname.itemname.

模块的“私有符号表”和函数“全局符号表”使我困惑。在

Symbol table: Wikipedia中,它写着:

The Python programming language includes extensive support for creating and manipulating symbol tables.[2] Properties that can be queried include whether a given symbol is a free variable or a bound variable, whether it is block scope or global scope, whether it is imported, and what namespace it belongs to.

看起来有点复杂。在

如何理解私有和全局符号表,我可以打印它们吗?在

按照PM 2Ring的评论,我创建一个脚本:

#symbol_table.py
print(global())
$ python symbol_table.py
{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x10bddf278>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': 'symbol_table.py', '__cached__': None}

{'__name__': '__main__', '__doc__': None, '是否被称为全局符号表?在


Tags: thetoinnoneyouistablevariables