0

我想将一个__getitem__方法从 python 转换为接受来自 cython 的数组。在这个特定的应用程序中,我应该允许在第二个索引中切片。对于 python,我可以写:

class name:
    def __init__(self):
        self.numbers = np.random.rand(2,3) 

    # ...
    # ...

    def __getitem__(self, tup):
        if type(tup) == tuple:
            x, y = tup
            return self.numbers[x][y]
        return self.numbers[tup]

一切都会很好。numbers但是,当我将变量声明为类型cdef public double[:,:] numbers并尝试使用 -object 调用-method__getitem__时,解释器不喜欢它。每当发生这种情况时,它都会抱怨TypeError: 'slice' object cannot be mapped as an integer。那么我的问题是:如果它是 a而不是整数,我将如何修改该行以便正确理解?yslicereturn self.bounds[x][y]yslice

4

0 回答 0