Skip to main content

33. Python Language Services

https://docs.python.org/3/library/language.html

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.

https://docs.python.org/3.8/library/symtable.html

dis - Disassembler for Python bytecode

The dis module supports the analysis of CPython bytecode by disassembling it. The CPython bytecode which this module takes as an input is defined in the fileInclude/opcode.hand used by the compiler and the interpreter.

Example: Given the function myfunc():

def myfunc(alist):
return len(alist)

the following command can be used to display the disassembly ofmyfunc():

dis.dis(myfunc)
2 0 LOAD_GLOBAL 0 (len)
2 LOAD_FAST 0 (alist)
4 CALL_FUNCTION 1
6 RETURN_VALUE
(The "2" is a line number).

myfunc.__code__ #here is the compiled bytecode stored
myfunc.__code__.co_code

https://docs.python.org/3/library/dis.html