Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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?

share|improve this question
add comment

2 Answers

up vote 4 down vote accepted

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).

share|improve this answer
Exactly. You could say a namespace is a symbol table that can be traversed with simple attribute access alone. –  Martijn Pieters Feb 25 '13 at 9:38
add comment

The symbol table means the same thing as for any compiler, that is, a data structure containing identifiers of a source code.
Modules, classes and functions constitute blocks of code having their own symbol table, in Python.

The term namespace is used in place of symbol table in Python doc of class SymbolTable, an instance of which gives access to the symbol table of a block of code as built by the specific interpreter you are using.

Hence, namespace and symbol table, in Python, relate to the same thing. The former is the symbol table as exposed to the developer, and the latter is the symbol table itself.
See the parallel between those two quotes taken from the Python doc:

"The local namespace for a function is created when the function is called"

"The execution of a function introduces a new symbol table used for the local variables of the function"

share|improve this answer
add comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.