回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>我有一个类<code>Board</code>。在</p>
<pre><code>class Board:
# Initialize empty n x n board
def __init__(self, n):
self.board = [[0]*n for x in range(n)]
self.size_n = n
# ...
</code></pre>
<p>我希望能够使用默认的<code>print(board_obj)</code>函数打印此类的对象,以便得到以下输出:</p>
^{pr2}$
<p>我试过了,但它只打印了一行矩阵,我不想:</p>
<pre><code># Represent the class object as its board
def __repr__(self):
return repr(self.board)
</code></pre>
<p>错误输出:</p>
<pre><code>[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
</code></pre>
<p>我也不希望使用下面这样的自定义类方法:</p>
<pre><code># Custom print
def print_board(self):
for row in self.board:
print row
</code></pre>