symtable --- コンパイラの記号表へのアクセス

ソースコード: Lib/symtable.py


記号表(symbol table)は、コンパイラが AST からバイトコードを生成する直前に作られます。記号表はコード中の全ての識別子のスコープの算出に責任を持ちます。 symtable はこうした記号表を調べるインターフェイスを提供します。

記号表の生成

symtable.symtable(code, filename, compile_type)

Python ソース code に対するトップレベルの SymbolTable を返します。 filename はコードを収めてあるファイルの名前です。 compile_typecompile()mode 引数のようなものです。

記号表の検査

class symtable.SymbolTable

ブロックに対する名前空間の記号表。コンストラクタはパブリックではありません。

get_type()

Return the type of the symbol table. Possible values are 'class', 'module', 'function', 'annotation', 'TypeVar bound', 'type alias', and 'type parameter'. The latter four refer to different flavors of annotation scopes.

バージョン 3.12 で変更: Added 'annotation', 'TypeVar bound', 'type alias', and 'type parameter' as possible return values.

get_id()

記号表の識別子を返します。

get_name()

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'). For type parameter scopes (which are used for generic classes, functions, and type aliases), it is the name of the underlying class, function, or type alias. For type alias scopes, it is the name of the type alias. For TypeVar bound scopes, it is the name of the TypeVar.

get_lineno()

この記号表に対応するブロックの一行目の行番号を返します。

is_optimized()

この記号表に含まれるローカル変数が最適化できるならば True を返します。

is_nested()

ブロックが入れ子のクラスまたは関数のとき True を返します。

has_children()

ブロックが入れ子の名前空間を含んでいるならば True を返します。入れ子の名前空間は get_children() で得られます。

get_identifiers()

Return a view object containing the names of symbols in the table. See the documentation of view objects.

lookup(name)

記号表から名前 name を見つけ出して Symbol インスタンスとして返します。

get_symbols()

記号表中の名前を表す Symbol インスタンスのリストを返します。

get_children()

入れ子になった記号表のリストを返します。

class symtable.Function

A namespace for a function or method. This class inherits from SymbolTable.

get_parameters()

この関数の引数名からなるタプルを返します。

get_locals()

この関数のローカル変数の名前からなるタプルを返します。

get_globals()

この関数のグローバル変数の名前からなるタプルを返します。

get_nonlocals()

この関数の非ローカル変数の名前からなるタプルを返します。

get_frees()

この関数の自由変数の名前からなるタプルを返します。

class symtable.Class

A namespace of a class. This class inherits from SymbolTable.

get_methods()

このクラスで宣言されているメソッド名からなるタプルを返します。

class symtable.Symbol

SymbolTable のエントリーでソースの識別子に対応するものです。コンストラクタはパブリックではありません。

get_name()

記号の名前を返します。

is_referenced()

記号がそのブロックの中で使われていれば True を返します。

is_imported()

記号が import 文で作られたものならば True を返します。

is_parameter()

記号がパラメータならば True を返します。

is_global()

記号がグローバルならば True を返します。

is_nonlocal()

記号が非ローカルならば True を返します。

is_declared_global()

記号が global 文によってグローバルであると宣言されているなら True を返します。

is_local()

記号がそのブロックに対してローカルならば True を返します。

is_annotated()

記号が注釈付きならば True を返します。

バージョン 3.6 で追加.

is_free()

記号がそのブロックの中で参照されていて、しかし代入は行われないならば True を返します。

is_assigned()

記号がそのブロックの中で代入されているならば True を返します。

is_namespace()

名前の束縛が新たな名前空間を導入するならば True を返します。

名前が関数または class 文のターゲットとして使われるならば、真です。

例えば:

>>> table = symtable.symtable("def some_func(): pass", "string", "exec")
>>> table.lookup("some_func").is_namespace()
True

一つの名前が複数のオブジェクトに束縛されうることに注意しましょう。結果が True であったとしても、その名前は他のオブジェクトにも束縛されるかもしれず、それがたとえば整数やリストであれば、そこでは新たな名前空間は導入されません。

get_namespaces()

この名前に束縛された名前空間のリストを返します。

get_namespace()

この名前に束縛された名前空間を返します。この名前に束縛された名前空間が複数あるか一つもないなら ValueError が送出されます。

Command-Line Usage

バージョン 3.13 で追加.

The symtable module can be executed as a script from the command line.

python -m symtable [infile...]

Symbol tables are generated for the specified Python source files and dumped to stdout. If no input file is specified, the content is read from stdin.