여기에 삽입 기능을 구현하려고합니다. 글쎄요, Grid 클래스의 자식 클래스 인이 클래스의 경우 문제가있는 것 같습니다. 그러나 나는 여기서 내가 잘못하고있는 것을 이해하지 못한다. 오류 메시지 : "Grid [m] [n] = value TypeError : 'type'객체는 subscriptable이 아닙니다." 파이썬 __getitem__ 오버로드 문제
감사
from Grid import Grid
class Matrix(Grid):
def __init__(self, m, n, value=None):
## super(Matrix, self).__init__(m, n)
Grid.__init__(self, m, n)
def insert(self, m, n, value=None):
Grid[m][n] = value
도와주세요 여기에 그리드 클래스
from CArray import Array
class Grid(object):
"""Represents a two-dimensional array."""
def __init__(self, rows, columns, fillValue = None):
self._data = Array(rows)
for row in xrange(rows):
self._data[row] = Array(columns, fillValue)
def getHeight(self):
"""Returns the number of rows."""
return len(self._data)
def getWidth(self):
"Returns the number of columns."""
return len(self._data[0])
def __getitem__(self, index):
"""Supports two-dimensional indexing
with [row][column]."""
return self._data[index]
def __str__(self):
"""Returns a string representation of the grid."""
result = ""
for row in xrange(self.getHeight()):
for col in xrange(self.getWidth()):
result += str(self._data[row][col]) + " "
result += "\n"
return result
왜'super' 호출을 주석 처리하고 직접'Grid' 호출로 대체 했습니까? '슈퍼'는 옳았다. 그리고 이전 질문에 대한 답을 받아주십시오. –
슈퍼 콜과 그리드 .__ init__이 같다고 생각합니다. 그것이 바로 이유입니다. 내가 틀렸다면 나를 바로 잡아주세요. – user1047092