How can I extract only the names of global variables referenced in a function or its code object?
I was using the co_names
attribute of code object base on this post, but then noticed this picks up any names other than the names of local or free variables. For example,
if foo
is defined as,
def foo():
return x.y
Then foo.__code__.co_names
yields ('x', 'y')
where I only want x
.