2

When reading 6. Modules, I encounter

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.

Module's 'private symbol table' and functions' 'global symbol table' confuse me.

In Symbol table: Wikipedia, it reads:

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.

It seems a little more complicated.

How to understand the private and global symbol table, could I print them?

Follow PM 2Ring's comment, I create a script:

#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}

Is {'__name__': '__main__', '__doc__': None, ' called global symble table?

AbstProcDo
  • 19,953
  • 19
  • 81
  • 138
  • 1
    There's the `globals()` function, which returns a dict of global names. And take a look at `dir(module_name)` – PM 2Ring May 10 '18 at 01:08
  • got it, thanks. Why not called global table and private table which facile to understand. @PM2Ring – AbstProcDo May 10 '18 at 01:17
  • Related post - [Symbol Table in Python](https://stackoverflow.com/q/9085450/465053) – RBT Aug 03 '18 at 06:35

0 Answers0