TypeError: Required argument not found __getitem__ numpy

2024-04-27 00:55:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我的目标是防止数组中出现负索引。在

import numpy as np

class Myarray (np.ndarray):
    def __getitem__(self,n):
       if n<0:
          raise IndexError("...")
       return np.ndarray.__getitem__(self,n)

class Items(Myarray):
    def __init__(self):
       self.load_tab()

class Item_I(Items):
    def load_tab(self):
       self.tab=np.load("file.txt")

a=Item_I()

创建实例时遇到错误:

^{pr2}$

Tags: importselfnumpy目标defnploaditems
1条回答
网友
1楼 · 发布于 2024-04-27 00:55:06

这是因为您从一个使用__new__来创建新实例的类的子类,^{} requires several arguments in ^{}甚至在它试图调用__init__之前:

Parameters for the __new__ method

shape : tuple of ints

Shape of created array.

dtype : data-type, optional

Any object that can be interpreted as a numpy data type.

buffer : object exposing buffer interface, optional

Used to fill the array with data.

offset : int, optional

Offset of array data in buffer.

strides : tuple of ints, optional

Strides of data in memory.

order : {‘C’, ‘F’}, optional

Row-major (C-style) or column-major (Fortran-style) order.

但是NumPy文档包含Subclassing ^{}的整个页面。在

您可能应该使用viewMyarray,而不是从Myarray子类化:

tab=np.load("file.txt")
tab.view(Myarray)

相关问题 更多 >