Python Glossary Русский

Python 3.2+disfunction

dis.code_info()

code information

Returns a string describing a code object: name, argument count, constants, global and local names.

Syntax

info = dis.code_info(x)

It returns a string and prints nothing — its twin show_code() does the printing. The most useful parts are Names and Variable names: the first lists names the function looks up globally, the second its locals. A name listed under Variable names is local throughout the whole body, which is where UnboundLocalError on a read before assignment comes from. A source string works too and is described as <module>.

Examples

import dis
def area(r):
    pi = 3.14
    return pi * r * r
info = dis.code_info(area)
print(type(info).__name__)  # → str
print(info.splitlines()[0])  # → Name:              area
print('Variable names:' in info)  # → True
import dis
print(dis.code_info('x = 1').splitlines()[0])  # → Name:              <module>

See also

Open in the full glossarydocs.python.orgReport an inaccuracy