我必须使用numpy记录数组来保存RAM并快速访问。但我想在该记录上使用成员函数。例如,
X=ones(3, dtype=dtype([('foo', int), ('bar', float)]))
X[1].incrementFooBar()
对于普通的python类,我可以制作
class QQQ:
...
def incrementFooBar(self):
self.foo+=1
self.bar+=1
pass
X=[QQQ(),QQQ(),QQQ()]
X[1].incrementFooBar()
我怎么能做出类似的东西,但对于numpy记录?
答案 0 :(得分:3)
我可能错了,但我认为没有办法在numpy数组中的记录上使用成员函数。或者,您可以非常轻松地构建一个函数来完成同样的事情:
X=ones(3, dtype=dtype([('foo', int), ('bar', float)]))
def incrementFooBar(X, index):
X['foo'][index] += 1
X['bar'][index] += 1
#then instead of "X[1].incrementFooBar()"
incrementFooBar(X, 1)