python/astT1.py

print('start', __name__, __file__)
import sys
print('version', sys.version, sys,sys.version_info)
import ast
try:
    a = compile('i + 5', '???', 'single', flags=ast.PyCF_ONLY_AST )
except BaseException as e:
    print('compile exception', e)
print(type(a), a, a.__dict__)

def show(n, l):
    print('  ' * l,  type(n), {k: v for k,v in ast.iter_fields(n)})
    for c in ast.iter_child_nodes(n):
        show(c, l+1)
show(a, 0)
# print('unparse', ast.unparse(a)) only in 3.9
c = compile(a,'???', 'single')
print('comp', c)
import code
print('globals', globals())
i = 33
code.InteractiveInterpreter(globals()).runcode(c)
code.InteractiveInterpreter({'i': 77}).runcode(c)
code.InteractiveInterpreter().runcode(c)

class cls:
    ci: int = 5
    cs: str = 'stringli'
    def fun(arg: int) -> int:
        global g
        v: str = 'v'
        w: int = 4
        v = 3
        return w*w

print('class', cls, 'anno', cls.__annotations__)
print('fun', cls.fun)
print('anno', cls.fun.__annotations__)