更改类对象的默认打印-Python

更改类对象的默认打印-Python,python,Python,我有一个班级板 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 # ... 我希望能够使用默认的print(board_obj)功能打印此类对象,以便获得以下输出: [0, 0, 0, 0, 0] [0, 0, 0, 0, 0] [0, 0, 0

我有一个班级

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
    # ...
我希望能够使用默认的
print(board_obj)
功能打印此类对象,以便获得以下输出:

[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]
我试过了,但它只打印了一行矩阵,我不想:

# Represent the class object as its board
def __repr__(self):
    return repr(self.board)
错误输出:

[[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]]
我也不想要下面这样的自定义类方法:

# Custom print
def print_board(self):
    for row in self.board:
        print row
那么:

def __str__(self):
    return '\n'.join(map(repr, self.board))

你应该使用
\uuuu str\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
作为人类可读的版本,而
\uuuu repr\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
作为明确的版本。

实现
默认情况下。有一个深入的讨论,但主要的一点是,
\uuuu repr\uuuu
应该是明确的,
\uu str\uuu
应该是人类可读的。