Python – How Does a Symbol Table Relate to a Namespace

python

The official tutorial uses the term symbol table in a few places where I would expect the term namespace.

1. Defining functions

The execution of a function introduces a new symbol table used for the
local variables of the function. More precisely, all variable
assignments in a function store the value in the local symbol table;
whereas variable references first look in the local symbol table, then
in the local symbol tables of enclosing functions, then in the global
symbol table, and finally in the table of built-in names.

2. More on 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.

I found Eli Bendersky's blog where he quotes the symtable module:

Symbol tables are generated by the compiler from AST just before
bytecode is generated. The symbol table is responsible for calculating
the scope of every identifier in the code.

So it seems like a symbol table precedes a namespace. Yet another quote, from the first source, leads me to believe they also exist at the same time.

The actual parameters (arguments) to a function call are introduced in
the local symbol table of the called function when it is called; thus,
arguments are passed using call by value (where the value is always an
object reference, not the value of the object).1

Is a symbol table involved with the creation of a namespace? Does a symbol table "contain" a namespace or simply information that a namespace contains?
In short,
how does a symbol table relate to a namespace?

Best Answer

A symbol table is an implementation detail. Namespaces are implemented using symbol tables, but symbol tables are used for more than just namespaces. For example, functions have their own symbol table for local variables, but those variables do not exist in any namespace (that is, it is impossible to somehow access the local variables of a function using a fully-qualified name).

Related Topic