In the python library pytides, I came across a strange method to initialize a class (the Tide class and its initialization ). I reproduced below a simplified version of the code :
import numpy as np
class Foo(object):
def __init__(self,x,y):
self.x = x
self.y = y
class Foobar(object):
dtype = np.dtype([('fooObj', object),
('A', float),
('B', float)])
def __init__(self,model):
'''model: an ndarray of type Foobar.dtype '''
self.model = model
# initialize a Foobar object
myFoos = [Foo(4,3),Foo(4,9),Foo(0,2)]
A = [2,3,4]
B = [8,9,0]
model = np.zeros(len(myFoos), dtype = Foobar.dtype)
model['fooObj'] = myFoos #what is that?!?
model['A'] = A
model['B'] = B
myFoobar = Foobar(model=model)
As I understand, the dtype variable in Foobar is a global variable, but I don't understand what's the point to have it. Is it just here to provide a convenient way to initialize Foobar? Moreover the Foobar class needs an array of Foobar.dtype at construction, is not a sort of cyclic call (which should crash)?