1:mod:`symtable` --- Access to the compiler's symbol tables
2==========================================================
3
4.. module:: symtable
5   :synopsis: Interface to the compiler's internal symbol tables.
6
7**Source code:** :source:`Lib/symtable.py`
8
9--------------
10
11.. moduleauthor:: Jeremy Hylton <[email protected]>
12.. sectionauthor:: Benjamin Peterson <[email protected]>
13
14
15Symbol tables are generated by the compiler from AST just before bytecode is
16generated.  The symbol table is responsible for calculating the scope of every
17identifier in the code.  :mod:`symtable` provides an interface to examine these
18tables.
19
20
21Generating Symbol Tables
22------------------------
23
24.. function:: symtable(code, filename, compile_type)
25
26   Return the toplevel :class:`SymbolTable` for the Python source *code*.
27   *filename* is the name of the file containing the code.  *compile_type* is
28   like the *mode* argument to :func:`compile`.
29
30
31Examining Symbol Tables
32-----------------------
33
34.. class:: SymbolTable
35
36   A namespace table for a block.  The constructor is not public.
37
38   .. method:: get_type()
39
40      Return the type of the symbol table.  Possible values are ``'class'``,
41      ``'module'``, and ``'function'``.
42
43   .. method:: get_id()
44
45      Return the table's identifier.
46
47   .. method:: get_name()
48
49      Return the table's name.  This is the name of the class if the table is
50      for a class, the name of the function if the table is for a function, or
51      ``'top'`` if the table is global (:meth:`get_type` returns ``'module'``).
52
53   .. method:: get_lineno()
54
55      Return the number of the first line in the block this table represents.
56
57   .. method:: is_optimized()
58
59      Return ``True`` if the locals in this table can be optimized.
60
61   .. method:: is_nested()
62
63      Return ``True`` if the block is a nested class or function.
64
65   .. method:: has_children()
66
67      Return ``True`` if the block has nested namespaces within it.  These can
68      be obtained with :meth:`get_children`.
69
70   .. method:: get_identifiers()
71
72      Return a view object containing the names of symbols in the table.
73      See the :ref:`documentation of view objects <dict-views>`.
74
75   .. method:: lookup(name)
76
77      Lookup *name* in the table and return a :class:`Symbol` instance.
78
79   .. method:: get_symbols()
80
81      Return a list of :class:`Symbol` instances for names in the table.
82
83   .. method:: get_children()
84
85      Return a list of the nested symbol tables.
86
87
88.. class:: Function
89
90   A namespace for a function or method.  This class inherits
91   :class:`SymbolTable`.
92
93   .. method:: get_parameters()
94
95      Return a tuple containing names of parameters to this function.
96
97   .. method:: get_locals()
98
99      Return a tuple containing names of locals in this function.
100
101   .. method:: get_globals()
102
103      Return a tuple containing names of globals in this function.
104
105   .. method:: get_nonlocals()
106
107      Return a tuple containing names of nonlocals in this function.
108
109   .. method:: get_frees()
110
111      Return a tuple containing names of free variables in this function.
112
113
114.. class:: Class
115
116   A namespace of a class.  This class inherits :class:`SymbolTable`.
117
118   .. method:: get_methods()
119
120      Return a tuple containing the names of methods declared in the class.
121
122
123.. class:: Symbol
124
125   An entry in a :class:`SymbolTable` corresponding to an identifier in the
126   source.  The constructor is not public.
127
128   .. method:: get_name()
129
130      Return the symbol's name.
131
132   .. method:: is_referenced()
133
134      Return ``True`` if the symbol is used in its block.
135
136   .. method:: is_imported()
137
138      Return ``True`` if the symbol is created from an import statement.
139
140   .. method:: is_parameter()
141
142      Return ``True`` if the symbol is a parameter.
143
144   .. method:: is_global()
145
146      Return ``True`` if the symbol is global.
147
148   .. method:: is_nonlocal()
149
150      Return ``True`` if the symbol is nonlocal.
151
152   .. method:: is_declared_global()
153
154      Return ``True`` if the symbol is declared global with a global statement.
155
156   .. method:: is_local()
157
158      Return ``True`` if the symbol is local to its block.
159
160   .. method:: is_annotated()
161
162      Return ``True`` if the symbol is annotated.
163
164      .. versionadded:: 3.6
165
166   .. method:: is_free()
167
168      Return ``True`` if the symbol is referenced in its block, but not assigned
169      to.
170
171   .. method:: is_assigned()
172
173      Return ``True`` if the symbol is assigned to in its block.
174
175   .. method:: is_namespace()
176
177      Return ``True`` if name binding introduces new namespace.
178
179      If the name is used as the target of a function or class statement, this
180      will be true.
181
182      For example::
183
184         >>> table = symtable.symtable("def some_func(): pass", "string", "exec")
185         >>> table.lookup("some_func").is_namespace()
186         True
187
188      Note that a single name can be bound to multiple objects.  If the result
189      is ``True``, the name may also be bound to other objects, like an int or
190      list, that does not introduce a new namespace.
191
192   .. method:: get_namespaces()
193
194      Return a list of namespaces bound to this name.
195
196   .. method:: get_namespace()
197
198      Return the namespace bound to this name. If more than one or no namespace
199      is bound to this name, a :exc:`ValueError` is raised.
200