设置
我正在用python编写一个类来处理一个二维的布尔数组。在class Grid(object):
def __init__(self, length):
self.length = length
self.grid = [[False]*length for i in range(length)]
def coordinates(self, index):
return (index // self.length, index % self.length)
有时在我的应用程序中,通过项目的坐标来访问它是有意义的,但有时通过它的索引访问一个项目更有意义。我还经常需要一次伪造或篡改一批物品。在不使课程变得复杂的情况下,我可以这样做:
^{pr2}$
第一步
当然,我想在我的Grid对象中添加一些mutator方法,这样我就不必直接访问对象内部并编写一堆循环:def set_coordinate(self, row, col, value):
self.grid[row][col] = bool(value)
def set_index(self, i, value):
coords = self.coordinates(i)
self.set_coordinates(coords[0], coords[1], value)
def set_coordinates(self, coordinates, value):
for row, col in coordinates:
self.set_coordinate(row, col, value)
def set_indices(self, indices, value):
for i