0

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.

4
  • 2
    This is very much an anti-pattern. Commented Jul 2, 2021 at 15:47
  • Those functions don't do anything. var is a local variable. Commented Jul 2, 2021 at 15:48
  • 1
    It's not possible to access local variables of a function from outside the function. Commented Jul 2, 2021 at 15:51
  • 1
    You should have the function return the value with return var. Then you can use var_values.append(function()) Commented Jul 2, 2021 at 15:53

2 Answers 2

1

A different way to achieve this, that is less of an anti-pattern and does not require introspection (also does not impose any specific function signature):

vars = { "foo_1": 1, "foo_2": 2, "foo_3": 3, "foo_4": 4 }

def foo_1():
    var = vars["foo_1"]
    return

def foo_2():
    var = vars["foo_2"]
    return

def foo_3():
    var = vars["foo_3"]
    return

def foo_4():
    var = vars["foo_4"]
    return

Some things to consider:

  • do these all need to be distinct functions if they have such similar implementations
  • since the functions seem to have a one to one mapping, can you use a list or some other data structure to map from vars to functions

Ultimately this is an XY Problem. You should add more context or outline the overall problem you are trying to solve.

Sign up to request clarification or add additional context in comments.

2 Comments

The only thing I agree with in this answer (not that the suggestion is wrong, just dubiously applicable) is the assertion that this is an XY problem.
That's what comments are for: to figure out what the real question is.
1

you can try something like

  • foo_1.__code__.co_varnames gives list of all local variables in the function
  • foo_1.__code__.co_consts gives all the constant values,first one being the default return value i.e. None
def foo_1():
    var = 1
    var2 = 2
    return

variables = dict(zip(foo_1.__code__.co_varnames, foo_1.__code__.co_consts[1:]))
print(variables)
{'var': 1, 'var2': 2}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.