3. symtable
— Access to the compiler’s symbol tables¶
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. symtable
provides an interface to examine these
tables.
3.1. Generating Symbol Tables¶
-
symtable.
symtable
(code, filename, compile_type)[source]¶ Return the toplevel
SymbolTable
for the Python source code. filename is the name of the file containing the code. compile_type is like the mode argument tocompile()
.
3.2. Examining Symbol Tables¶
-
class
symtable.
SymbolTable
[source]¶ A namespace table for a block. The constructor is not public.
-
get_type
()[source]¶ Return the type of the symbol table. Possible values are
'class'
,'module'
, and'function'
.
-
get_name
()[source]¶ Return the table’s name. This is the name of the class if the table is for a class, the name of the function if the table is for a function, or
'top'
if the table is global (get_type()
returns'module'
).
-
has_children
()[source]¶ Return
True
if the block has nested namespaces within it. These can be obtained withget_children()
.
-
-
class
symtable.
Function
[source]¶ A namespace for a function or method. This class inherits
SymbolTable
.
-
class
symtable.
Class
[source]¶ A namespace of a class. This class inherits
SymbolTable
.
-
class
symtable.
Symbol
[source]¶ An entry in a
SymbolTable
corresponding to an identifier in the source. The constructor is not public.-
is_namespace
()[source]¶ Return
True
if name binding introduces new namespace.If the name is used as the target of a function or class statement, this will be true.
For example:
>>> table = symtable.symtable("def some_func(): pass", "string", "exec") >>> table.lookup("some_func").is_namespace() True
Note that a single name can be bound to multiple objects. If the result is
True
, the name may also be bound to other objects, like an int or list, that does not introduce a new namespace.
-
get_namespace
()[source]¶ Return the namespace bound to this name. If more than one namespace is bound, a
ValueError
is raised.
-