Home > Net >  Override setter for multidimensional array
Override setter for multidimensional array

Time:09-29

I am trying to override setter and decorate it, but don't understand how it works for multidimensional array.

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]

I found out, that setter/getter doesn't work as I expect

>>> 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

I expected, that setter will work here, but I think that getter returns list and modifies element of that list.

>>> 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 works only if I use no more than 1 index.

CodePudding user response:

I think it might be simpler to do things using a dictionary-of-values instead of a list-of-lists-of-values to do things. In this case what you called the item argument to the __setitem__() and __getitem__() methods has been renamed to indices in the sample code below since it's now a tuple of integer values: (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)

Output:

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
  • Related