Python symtable: inspect the compiler's identifier scopes
Answer: The symtable module lets you inspect the compiler's symbol tables, which decide the scope of every identifier in the code.
What a symbol table records
The compiler builds symbol tables that calculate the scope of every identifier.
This module is the public interface for examining those tables.
SymbolTableType names the kinds of tables, including module, function, and class.
Other members cover annotation scopes, type aliases, and type parameters used by generic functions or generic classes.
The annotation kind is used when from __future__ import annotations is active.
Looking up names
A SymbolTable is a namespace table for a block.
get_name returns the class or function name, or top when the table is global and get_type returns module.
A Symbol is one entry in that table for an identifier in the source.
is_namespace is true when the name is the target of a function or class statement.
Version 3.15 added an optional module parameter used to filter syntax warnings by module name.
In version 3.16, the code argument can be an AST object.
See also
Claims
- id: C1 claim: The symbol table calculates the scope of every identifier in the code. source: S1 evidence: "responsible for calculating the scope of every identifier in the code"
- id: C2 claim: SymbolTableType includes module, function, class, and annotation scopes. source: S1 evidence: "Used for the symbol table of a module"
- id: C3 claim: In version 3.16, code can be an AST object. source: S2 evidence: "can now be an AST object"
Sources
- [S1] symtable: Interface to the compiler's symbol tables. Python Software Foundation. https://docs.python.org/3/library/symtable.html . License: PSF License Version 2. Accessed 2026-11-26.
- [S2] symtable documentation (source). CPython. https://raw.githubusercontent.com/python/cpython/main/Doc/library/symtable.rst . License: PSF License Version 2. Accessed 2026-11-26.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Python Software Foundation, symtable: Interface to the compiler's symbol tables; CPython, symtable documentation (source). PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Python Software Foundation, symtable: Interface to the compiler's symbol tables; CPython, symtable documentation (source). PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation.
Cite this
iinventedlight. 2026. "Python symtable: inspect the compiler's identifier scopes." iinventedlight Library, iil:note:k1pra2s4, version of 2026-11-26. https://iinventedlight.com/library/notes/python-symtable/. License: CC-BY-4.0.
@misc{iil_note_k1pra2s4,
author = {{iinventedlight}},
title = {{Python symtable: inspect the compiler's identifier scopes}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-26},
url = {https://iinventedlight.com/library/notes/python-symtable/},
note = {Permanent ID iil:note:k1pra2s4. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
Extra files beside this note
These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.
- python-symtable.schema.json (schema; CC-BY-4.0)
- python-symtable.table.json (table; CC-BY-4.0)
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/python-symtable.md.