我正在尝试实现 numpy recarray (recsub) 的子类并将其实例分配给 dtype 'object' (ndarr) 的 ndarray。它工作得很好,但是当子类的recarray用一个空数组实例化时我遇到了一个问题。这是子类 recarry 的代码:
class recsub(numpy.recarray):
"""subclassed recarray"""
def __new__(cls, *args, **kwargs):
obj = numpy.recarray.__new__(cls, *args, **kwargs)
return obj
def __init__(self, *arg, **kwargs):
self.x = -1
def new_method(self):
print 'new_method() : fooooooooooooo'
我将ndarray创建为:
ndarr = numpy.ndarray(5, 'object')
现在,如果我创建两个 recsub 实例:
ndarr[0] = recsub(2, [('a','f8')])
ndarr[1] = recsub((), [('a','f8')])
现在这是正在发生的奇怪的事情。的输出:
print type(ndarr[0])
print type(ndarr[1])
是:
>>> <class '__main__.recsub'>
>>> <class 'numpy.core.records.record'>
所以我无法访问 ndarr[1].x
这曾经在 numpy 1.7 中有效,但在 numpy 1.8 中不再有效!因此,在使用形状 () 而不是 (n) 实例化重新数组时,似乎缺少一些东西
欢迎任何建议,
tnx 提前,