blob: c9521d649b893e36aa586299c8c3559d3427071d [file] [log] [blame]
Benjamin Peterson55e00f22008-08-17 18:02:44 +00001: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
Georg Brandlc0a8f8c2014-10-02 08:38:39 +02007**Source code:** :source:`Lib/symtable.py`
8
9--------------
10
Benjamin Peterson55e00f22008-08-17 18:02:44 +000011.. moduleauthor:: Jeremy Hylton <jeremy@alum.mit.edu>
Benjamin Petersonaa069002009-01-23 03:26:36 +000012.. sectionauthor:: Benjamin Peterson <benjamin@python.org>
Benjamin Peterson55e00f22008-08-17 18:02:44 +000013
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
Benjamin Peterson55e00f22008-08-17 18:02:44 +000070 .. method:: get_identifiers()
71
72 Return a list of names of symbols in this table.
73
74 .. method:: lookup(name)
75
76 Lookup *name* in the table and return a :class:`Symbol` instance.
77
78 .. method:: get_symbols()
79
80 Return a list of :class:`Symbol` instances for names in the table.
81
82 .. method:: get_children()
83
84 Return a list of the nested symbol tables.
85
86
87.. class:: Function
88
89 A namespace for a function or method. This class inherits
90 :class:`SymbolTable`.
91
92 .. method:: get_parameters()
93
Benjamin Petersonb71caf12008-08-20 12:55:31 +000094 Return a tuple containing names of parameters to this function.
Benjamin Peterson55e00f22008-08-17 18:02:44 +000095
96 .. method:: get_locals()
97
Benjamin Petersonb71caf12008-08-20 12:55:31 +000098 Return a tuple containing names of locals in this function.
Benjamin Peterson55e00f22008-08-17 18:02:44 +000099
100 .. method:: get_globals()
101
Benjamin Petersonb71caf12008-08-20 12:55:31 +0000102 Return a tuple containing names of globals in this function.
Benjamin Peterson55e00f22008-08-17 18:02:44 +0000103
Pablo Galindod5b4f1b2018-10-20 01:46:00 +0100104 .. method:: get_nonlocals()
105
106 Return a tuple containing names of nonlocals in this function.
107
Benjamin Peterson55e00f22008-08-17 18:02:44 +0000108 .. method:: get_frees()
109
Benjamin Petersonb71caf12008-08-20 12:55:31 +0000110 Return a tuple containing names of free variables in this function.
Benjamin Peterson55e00f22008-08-17 18:02:44 +0000111
112
113.. class:: Class
114
115 A namespace of a class. This class inherits :class:`SymbolTable`.
116
117 .. method:: get_methods()
118
Benjamin Petersonb71caf12008-08-20 12:55:31 +0000119 Return a tuple containing the names of methods declared in the class.
Benjamin Peterson55e00f22008-08-17 18:02:44 +0000120
121
122.. class:: Symbol
123
124 An entry in a :class:`SymbolTable` corresponding to an identifier in the
125 source. The constructor is not public.
126
127 .. method:: get_name()
128
129 Return the symbol's name.
130
131 .. method:: is_referenced()
132
133 Return ``True`` if the symbol is used in its block.
134
135 .. method:: is_imported()
136
137 Return ``True`` if the symbol is created from an import statement.
138
139 .. method:: is_parameter()
140
141 Return ``True`` if the symbol is a parameter.
142
143 .. method:: is_global()
144
145 Return ``True`` if the symbol is global.
146
Pablo Galindod5b4f1b2018-10-20 01:46:00 +0100147 .. method:: is_nonlocal()
148
149 Return ``True`` if the symbol is nonlocal.
150
Benjamin Peterson0289b152009-06-28 17:22:03 +0000151 .. method:: is_declared_global()
152
153 Return ``True`` if the symbol is declared global with a global statement.
154
Benjamin Peterson55e00f22008-08-17 18:02:44 +0000155 .. method:: is_local()
156
157 Return ``True`` if the symbol is local to its block.
158
Joannah Nanjekyea95ac772020-07-07 20:09:56 -0300159 .. method:: is_annotated()
160
161 Return ``True`` if the symbol is annotated.
162
Benjamin Peterson55e00f22008-08-17 18:02:44 +0000163 .. method:: is_free()
164
165 Return ``True`` if the symbol is referenced in its block, but not assigned
166 to.
167
168 .. method:: is_assigned()
169
170 Return ``True`` if the symbol is assigned to in its block.
171
172 .. method:: is_namespace()
173
174 Return ``True`` if name binding introduces new namespace.
175
176 If the name is used as the target of a function or class statement, this
177 will be true.
178
Benjamin Petersone0124bd2009-03-09 21:04:33 +0000179 For example::
180
181 >>> table = symtable.symtable("def some_func(): pass", "string", "exec")
182 >>> table.lookup("some_func").is_namespace()
183 True
184
Benjamin Peterson55e00f22008-08-17 18:02:44 +0000185 Note that a single name can be bound to multiple objects. If the result
186 is ``True``, the name may also be bound to other objects, like an int or
187 list, that does not introduce a new namespace.
188
189 .. method:: get_namespaces()
190
191 Return a list of namespaces bound to this name.
192
193 .. method:: get_namespace()
194
195 Return the namespace bound to this name. If more than one namespace is
Benjamin Petersonad643b52015-05-06 21:29:14 -0400196 bound, :exc:`ValueError` is raised.