How can I store a function in an array in python?

74 views Asked by At

I am trying to to store a function in an array and store that array in a dictionary so I can later call values from that array through the dictionary.

I created a zeros array and created a loop that will store the function in the array as the loop goes on. Every cell in zeros array should contain the function but with different arguments depending on the loop.

I tried this:

Note, in the code, the function is "HeatedSolid" from a python package called SubKit which I imported as "sk"

Also note that the function does not produce an integer or float or any number its just a function that produces an argument that I will process using a different code.

Example: if you pass arguments to the function,

sk.HeatedSolid(I put random put arguments here) returns the following argument: <SubKit.build.Solid.HeatedSolid object at 0x2b407581b250>

matrix = some array

X_position = some array
Y_position = some array

matrix_dict = {
    10: {"multi": "69"},
    20: {"multi": "59"}, 
    30: {"multi": "49"}
}
array = np.zeros((10, 10))

for i in range(len(matrix)):
    for j in range(len(matrix[i])):
        if matrix[i,j] == "10":
            if matrix[i,j] in matrix_dict.keys():
                matrix_params = matrix_dict[matrix[i,j]]
                array[i][j]= sk.HeatedSolid(x=X_position[i][j],
                                            y=Y_position[i][j],
                                            symtype= 1,
                                            mult=rod_params("multi"),
                                            power=1)
print(array)

When I print the array its just a zeros array and the function is not store in the array

2

There are 2 answers

5
Yucheng Zhou On

If you want to store a function itself (a callable thing) in an array, you should use a python list rather than a numpy's ndarray (where the later can only store primitive int or float or string value).
In your case, you want to store function and its arguments, so you can do something like that:


matrix_dict = {
    10: {"multi": "69"},
    20: {"multi": "59"}, 
    30: {"multi": "49"}
}

array = []  # note here
for i in range(len(matrix)):
    array_row = []  # note here
    for j in range(len(matrix[i])):
        if matrix[i,j] == "10":
            if matrix[i,j] in matrix_dict.keys():
                matrix_params = matrix_dict[matrix[i,j]]
                array_row.append((sk.HeatedSolid, {"x":X_position[i][j],
                                                "y":Y_position[i][j],
                                                "symtype": 1,
                                                "mult":rod_params("multi"),
                                                "power":1}))  # note here, append a tuple of function and its args
    array.append(array_row)  # note here

# when you want to call it later
for arr in array:
    for (func, kwargs) in arr:
        func(**kwargs)
2
user23622626 On

Instead of a normal zeros array I used this:

array = np.zeros_like(matrix, dtype=object)

I just to had define that the zeros array takes object since the function produces an object.