Get global context variables used by a function in Python

def get_function_globals(fn):
    return (*(
            fn.__globals__[instruction.argval]
            for instruction
            in dis.Bytecode(fn)
            if instruction.opname == 'LOAD_GLOBAL'
        ),)

Requirements:

import dis

Related: