I have a Python script called module.py with several functions in it, like this:
def foo_1():
var = 1
return
def foo_2():
var = 2
return
def foo_3():
var = 3
return
def foo_4():
var = 4
return
Each of the functions (foo_1, foo_2, ...) defines a unique value for the variable named var, and I am looking for a way to grab this value from each of the functions. Is this possible in Python? It is crucial to my project that each of the variables var have the same local name in their respective function.
I have already been able to grab a list of all the functions as follows:
from inspect import getmembers, isfunction
import module
function_list = [pair[0] for pair in getmembers(module, isfunction)]
Which returns a list of all the functions names [foo_1, foo_2, foo_3, foo_4]. Now, I am looking for a way to iterate through all the functions and grab the value of var in each one — something like this:
var_value_tuples = []
for function in function_list:
var_values.append((function, function.var))
Thank you in advance for any help.
varis a local variable.return var. Then you can usevar_values.append(function())