How to check the reference to global variables in function?

As a scientific computing practitioner, I usually write sequential code and debug it in Jupyter. Once I complete the target, I encapsulate the code into a function for parallel execution.

Therefore, I need to collect all necessary variables and write them to a parameters list, but it is easy to miss some parameters. So, I hope that tools (perhaps linting tools) can notify me if there are any references to global variables in the function.

Now, I copy the function code and imports to a new file in VSCode and wait for red wavy lines, which can be a bit troublesome.

In case someone wants to know more:

# Initially, I debug some calculation code
a = 1
b = 2
c = 3

result = a**b - c
# After completing them, I would collect the parameters.
from joblib import Parallel, delayed

def f(a: float, b: float): 
    #                Oh, I forgot the `c`.
    result = a**b - c
    return result

Parallel(n_jobs=16)(delayed(f)(a, b) for a, b in zip(aa, bb))
# Then, c is always a constant value.

Thank you in advance

That’s an interesting question. You don’t need to use special packages for this (well, depends on how exactly you want to run all this).
You can get all the names (names of global variables and for instance functions called from inside a given function), by inspecting: f.__code__.co_names.
For instance in your case you would get

f.__code__.co_names == ('c',)

But if f was calling some other function g, then co_names would be ('g', 'c').
If your functions never call other functions then this is no problem, otherwise you may want to filter those out (since you already have the list of functions? otherwise there are ways to do so too).
See: 3. Data model — Python 3.12.0 documentation

3 Likes

Thank you, this is a nice method; f.__code__.co_names has reduced many checks. At least, afterward, there is no need to copy imports to a new file.