为多维数组覆盖 setter

Override setter for multidimensional array

我正在尝试覆盖 setter 并装饰它,但不明白它如何用于多维数组。

class Board:
    def __init__(self):
        self.board = []
        for k in range(3):
            self.board.append([0] * 3)

    def __str__(self):
        return '\n'.join([' '.join([str(num) for num in row]) for row in self.board])

    def __repr__(self):
        return self.__str__()

    def __setitem__(self, item, value):
        print(f'setting {item=}\t\t{value=}')
        self.board[item] = value

    def __getitem__(self, item):
        print(f'getting {item=}')
        return self.board[item]

我发现 setter/getter 没有像我预期的那样工作

>>> board = Board()
>>> print(board)
0 0 0
0 0 0
0 0 0
>>> print(board[1][1])
getting item=1
0
>>> board[1][1] = 3
getting item=1

我预计 setter 会在这里工作,但我认为 getter returns 列出并修改该列表的元素。

>>> print(board)
0 0 0
0 3 0
0 0 0
>>> board[0] = [1, 3, 1]
setting item=0      value=[1, 3, 1]
>>> print(board)
1 3 1
0 3 0
0 0 0

Setter 仅当我使用不超过 1 个索引时才有效。

我认为使用 dictionary-of-values 而不是 list-of-lists-of-values[= 做事可能更简单24=]。在这种情况下,您调用 __setitem__()__getitem__() 方法的 item 参数在下面的示例代码中已重命名为 indices,因为它现在是 tuple整数值:(row, col).

class Board:
    H, W = 3, 3  # Size in rows (height) X columns (width).

    def __init__(self):
        self.board = {(row, col): 0 for col in range(self.H)
                                        for row in range(self.W)}

    def __str__(self):
        return '\n'.join(', '.join(str(self[row, col])
                                for col in range(self.H))
                                    for row in range(self.W))

    __repr__ = __str__

    def __setitem__(self, indices, value):
        self.board[indices] = value

    def __getitem__(self, indices):
        return self.board[indices]


if __name__ == '__main__':
    board = Board()
    print(board)
    print(f'{board[1, 2]=}')
    print()

    board[1, 2] = 3
    print(f'{board[1, 2]=}')
    print(board)

输出:

0, 0, 0
0, 0, 0
0, 0, 0
board[1, 2]=0

board[1, 2]=3
0, 0, 0
0, 0, 3
0, 0, 0