如何修复AttributeError:'Matrix'对象没有属性'clear'?

时间:2019-10-12 14:51:03

标签: python arrays

我正在尝试创建一个Matrix类。但是,创建它时会抛出错误。

我试图删除self._theGrid.clear(0),但矩阵将初始化为None而不是0。

from array_ import Array2D

class Matrix :
    # Creates a matrix of size numRows x numClos initialized to 0.
    def __init__(self, numRows, numCols):
        self._theGrid = Array2D(numRows, numCols)
        self._theGrid.clear(0)
    ....

在我创建的模块中。我正在使用以下模块创建矩阵。


# Implement the Array ADT using the array capabilities of the ctypes module
import ctypes

class Array:
     # Create array with the size elements
    def __init__(self, size):
        assert size > 0, "Array size must be > 0"
        self._size = size
         # Create the array structure using ctypes module
        PyArrayType = ctypes.py_object * size
        self._elements = PyArrayType()
         # Initialize each element
        self.clear(None)

     # Returns the size of the array
    def __len__(self):
        return self._size

     # Get the contents of the index element
    def __getitem__(self, index):
        assert index >= 0 and index < len(self), "Array subscripts out of range"
        return self._elements[index]

      # Puts the value in the array element at index position.
    def __setitem__(self, index, value):
        assert index >= 0 and index < len(self), "Array subscripts out of range"
        self._elements[index] = value

     # Clear the array by setting each element to the given value.
    def clear (self, value):
        for i in range(len(self)):
            self._elements[i] = value
     # Returns the array's ierator for transversing the elements.
    def __iter__(self):
        return _ArrayIterator( self._elements )
# An iterator for the Array ADT.
class _ArrayIterator:

    def __init__(self, theArray):
        self._arrayRef = theArray
        self._curNdx = 0

    def __iter__(self):
        return self

    def __next__(self):
        if self._curNdx < len(self._arrayRef):
            entry = self._arrayRef[self._curNdx]
            self._curNdx += 1
            return entry
        else:
            raise StopIteration

# Implementation of the Array2D ADT using an array of arrays
class Array2D:
    # Creates a 2-D array of size numRows x numCols 
    def __init__(self, numRows, numCols):
        # Create a 1-D array to store an array reference for each row.
        self._theRows = Array(numRows)

        # Create the 1-D arrays for each row of the 2-D array. 
        for i in range(numRows):
            self._theRows[i] = Array(numCols)

    # Return the number of rows of the 2-D array. 
    def numRows(self):
        return len(self._theRows)

    # Return the number of columns of the 2-D array. 
    def numCols(self):
        return len(self._theRows[0]) 

    # Clear the arrays by setting every element to the given value
    def clear(self, value):
        for row in range(self.numRows()):
            row.clear(value)

    # Gets the contents of the element at position [i,j]
    def __getitem__(self, ndxTuple):
        assert len( ndxTuple ) == 2, "Invalid number of array subscripts"
        row = ndxTuple[0]
        col = ndxTuple[1]
        assert  row >= 0 and row < self.numRows() and col >=0 and col < self.numCols(), "Array subscripts out of range"
        the1dArray = self._theRows[row]
        return the1dArray[col]

    # Set the contents of the element at position [i,j] to value.
    def __setitem__(self, ndxTuple, value):
        assert len( ndxTuple ) == 2, "Invalid number of array subscripts"
        row = ndxTuple[0]
        col = ndxTuple[1]
        assert  row >= 0 and row < self.numRows() and col >=0 and col < self.numCols(), "Array subscript out of range"
        the1dArray = self._theRows[row]
        the1dArray[col] = value

我无法获得所有矩阵项都初始化为0的结果。

第8行,初始化 self._theGrid.clear(0)

第77行,明晰 row.clear(value)

AttributeError:“矩阵”对象没有属性“清除”

我无法使用x = Matrix(2,3)初始化矩阵。

1 个答案:

答案 0 :(得分:1)

似乎您要遍历数组的长度:

...
def clear( self, value ):
    for row in range( self.numRows() ):  # row is 0, 1, 2, ...  
        row.clear( value )               # int has no method or attribute with name "clear" -> error is raised
...

尝试遍历数组:

...
def clear( self, value ):
    for row in self._theRows:
        row.clear( value ) 
...