1

那个设定

我正在用 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)

有时在我的应用程序中,通过坐标访问项目是有意义的,但有时通过索引访问项目更有意义。我还经常需要一次伪造或真实化一批项目。在不使课程非常复杂的情况下,我可以这样做:

g = Grid(8)

# access by coordinates
g.grid[4][3] = True

# access by index
coords = g.coordinates(55)
g[coords[0]][coords[1]] = True

# truthify a batch of coordinates
to_truthify = [(1, 3), (2, 3), (2, 7)]
for row, col in to_truthify:
    g.grid[row][col] = True

# falsify a batch of indices
to_falsify = [19, 22, 60]
for i in to_falsify:
    coords = g.coordinates(i)
    g.grid[coords[0]][coords[1]] = False

第一步

自然地,我想在我的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 in indices:
        self.set_index(i, value)

访问器方法也很简单。我可能还想添加一些语义上有意义的别名:

def truthify_coordinate(self, row, col):
    self.set_coordinate(row, col, True)

def falsify_coordinate(self, row, col):
    self.set_coordinate(row, col, False)

def truthify_coordinates(self, coordinates):
    self.set_coordinates(coordinates, True)

... etc ...

理念

例如,我想创建一个名为 的方法set_item,其中位置可以是长度为 2 的表示坐标的可迭代对象,可以是标量索引。

def set_item(self, location, value):
    try:
        location = self.coordinates(location)
    except TypeError:
        pass
    self.set_coordinates(location[0], location[1], value)

优点和缺点

这样做的好处(显然)是我不需要指定位置是一对坐标还是索引,所以当我一次设置一批位置时,它们不需要都是相同的时间。例如,以下内容:

indices = [3, 5, 14, 60]
coordinates = [(1, 7), (4, 5)]
g.truthify_indices(indices)
g.truthify_coordinates(coordinates)

变成

locations = [3, 5, (1, 7), 14, (4, 5), 60]
g.truthify(locations)

在我看来,这更清晰,更容易阅读和理解。

缺点之一是类似g.truthify((2, 3))的东西更难立即破译(是设置坐标还是两个索引?)。可能还有更多我没有想到的。

问题

实现这个想法是pythonic要做的事情,还是我应该坚持明确区分索引和坐标?

4

1 回答 1

4

我认为一种更 Pythonic 的编写方式:

g.truthify_coordinate(row, col)

是能够写:

g[row][col] = True   # or g[row, col] = True

第二个在阅读时更容易理解,这是 Python 更重要的准则之一:阅读代码的频率远高于编写代码的频率。(尽管我远非 Pythonic 的专家。)

也就是说,您似乎正在重新开发numpy,并且在大多数情况下重新实现一个好的工具是错误的。可能有不使用 numpy 的原因,但应该先探索一下。此外,如果您选择不使用 numpy,它的设计将为您提供自己的方法的想法。

于 2013-10-05T01:43:21.847 回答