0

I've been trying to figure this out for multiple days now.. Basically I have a large list of functions that I want to wrap in another function, and then redefine the original function as that wrapped function; but to do so in a list. Example:

Wrapper = lambda f: lambda x,y: f(x+3, y+3)
Funcs = f1, f2, f3, f4

f1, f2, f3, f4 = np.vectorize(Wrapper)(Funcs) ### Works
Funcs = np.vectorize(Wrapper)(Funcs) ### Fails
*Funcs, = np.vectorize(Wrapper)(Funcs) ### Fails
*[f1, f2, f3, f4], = np.vectorize(Wrapper)(Funcs) ### Fails
[f1, f2, f3, f4] = np.vectorize(Wrapper)(Funcs) ### Fails

### Defining them as a list, tuple, or dict doesn't seem to help either
### Also attempted other approaches than np.vectorize, but all fail the same.

Also attempted iterators/maps, list/dict comprehensions, for loops, attempts to redefine function name, and even trying to redefine the functions in a loop as global variables - all fail. I've attempted every single looping/iteration python method I am aware of or that I can find reference to online.

Basically, why am I not able to redefine/reassign functions in a list/tuple/dict in a looping process? Anytime loop I attempt will execute without error, but when I go to call the function, it just calls the old function and not the redefined one - unless I redefine the functions in an explicitly stated way as indicated above.

Is there any way I can pass variables as memory address pointers? I am on the verge of trying to use Ctypes to remap the function by memory address, but I find it hard to believe that is the only option here.

  • 3
    NOTHING other than an actual assignment to `f1`, etc. will change their values. `Funcs` is a tuple containing the values from the four individual variables; it is not a tuple of *the variables themselves*, and gives you absolutely no ability to change the variables. Having a bunch of variables with names differing only in a number is usually a sign that you should have had a single list (or other container), rather than individual variables. – jasonharper Jun 25 '20 at 21:51
  • 3
    Can we take a step back here? What is this for? – AMC Jun 25 '20 at 21:52
  • Related: [How do I create a variable number of variables?](https://stackoverflow.com/q/1373164/4518341) Might be a duplicate depending on what you're trying to accomplish. – wjandrea Jun 25 '20 at 21:56

0 Answers0