If I have an array like:
a = np.array([ [A(2,3 , np.array([[C(2,3)], [C(5,6)] ]))],
[A(4,5 , np.array([[C(1,2)],[C(9,7)]]))]
])
with other class instances, how can I access all the elements?
For example,
for idx,x in np.ndenumerate(a):
print('Index :{0}'.format(idx))
print('Elmt: {0}'.format(x.the_c[idx].t))
returns:
Index :(0, 0)
Elmt: 2
Index :(1, 0)
Elmt: 9
so only 2 indices and 2 elements instead of 4.
Normally, I have to call another ndenumerate but I am not sure how to call it or if there is a better (more efficient) way.
The code:
import numpy as np
class A():
def __init__(self, a, b,the_c):
self.a = a
self.b = b
self.the_c = the_c
def methodA(self):
return self.the_c
class B():
def __init__(self, c, d, the_a):
self.c = c
self.d = d
self.the_a = the_a
def evaluate(self):
for idx, x in np.ndenumerate(self.the_a):
x.methodA()
return x.the_c
class C():
def __init__(self,t,y):
self.t = t
self.y = y
And if I want to evaluate the a array by calling a function, how can I call it?
def evaluate(self):
for idx, x in np.ndenumerate(self.the_a):
x.methodA()
return x.the_c
OR
def evaluate(self):
for idx, x in np.ndenumerate(self.the_a):
the_inst = A(x.a, x.b, x.self.the_c)
the_inst.methodA()
return the_inst.the_c
So, the evaluate method in class B will be the only one that gets called and it will execute the many A instances which contain the many C instances.
a = np.array([ [A(2,3 , np.array([[C(2,3)], [C(5,6)] ]))],
[A(4,5 , np.array([[C(1,2)],[C(9,7)]]))]
])
b = B(1,2,a).evaluate()
for idx, x in np.ndenumerate(b):
print(x.t)
which gives 2 and 5 instead of 2,5,1,9.
reprmethods as I suggested for stackoverflow.com/questions/41985458/…