Consider the following two implemtations of the same piece of code. I would have thought they are identical but they are not.
Is this a Python/Numpy bug or a subtle gotcha? If the latter, what rule would make it obvious why it does not work as expected?
I was working with multiple arrays of data and having to process each array item by item, with each array manipulated by a table depending on it's metadata.
In the real world example 'n' is multiple factors and offsets but the following code still demonstrates the issue that I was getting the wrong result in all but one case.
import numpy as np
# Change the following line to True to show different behaviour
NEEDS_BUGS = False  # Changeme
# Create some data
data = np.linspace(0, 1, 10)
print(data)
# Create an array of vector functions each of which does a different operation on a set of data
vfuncd = dict()
# Two implementations
if NEEDS_BUGS:
    # Lets do this in a loop because we like loops - However WARNING this does not work!!
    for n in range(10):
        vfuncd[n] = np.vectorize(lambda x: x * n)
else:
    # Unwrap the loop - NOTE: Spoiler - this works
    vfuncd[0] = np.vectorize(lambda x: x * 0)
    vfuncd[1] = np.vectorize(lambda x: x * 1)
    vfuncd[2] = np.vectorize(lambda x: x * 2)
    vfuncd[3] = np.vectorize(lambda x: x * 3)
    vfuncd[4] = np.vectorize(lambda x: x * 4)
    vfuncd[5] = np.vectorize(lambda x: x * 5)
    vfuncd[6] = np.vectorize(lambda x: x * 6)
    vfuncd[7] = np.vectorize(lambda x: x * 7)
    vfuncd[8] = np.vectorize(lambda x: x * 8)
    vfuncd[9] = np.vectorize(lambda x: x * 9)
# Prove we have multiple different vectorised functions
for k, vfunc in vfuncd.items():
    print(k, vfunc)
# Do the work
res = {k: vfuncd[k](data) for k in vfuncd.keys()}
# Show the result
for k, r in res.items():
    print(k, r)
 
     
     
    