Python 无法监视列表“矩阵”

Python 无法监视列表“矩阵”,python,function,class,class-method,Python,Function,Class,Class Method,我对类CellList有问题,所以我想打印列表矩阵。但当我打印此列表时,我的Pycharm显示[[myfile.Cell对象在0x0034…][myfile.Cell对象在0x0034…],] class Cell: def __init__(self, row: int, col: int, state=0): self.state = state self.row = row self.col = col def is_al

我对类CellList有问题,所以我想打印列表矩阵。但当我打印此列表时,我的Pycharm显示[[myfile.Cell对象在0x0034…][myfile.Cell对象在0x0034…],]

class Cell:

    def __init__(self, row: int, col: int, state=0):
        self.state = state
        self.row = row
        self.col = col

    def is_alive(self) -> bool:
        return self.state


class CellList():

    @classmethod
    def from_file(cls, filename):
        with open(filename, 'r') as f:

            r = 0
            matrix = []

            for line in f:
                c = 0
                arr = []
                for ch in line:
                    if ch == '0' or ch == '1':
                        arr.append(Cell(r, c, int(ch)))
                    c += 1
                matrix.append(arr)
                r += 1

        cls.r_max = r
        cls.c_max = c - 1
        cls.matrix = matrix

        return CellList(cls.r_max, cls.c_max, cls.matrix)

为了让一个对象以非默认的方式表示它自己,您必须使用一个方法。这种方法可能是:

class Cell:
    # ....
    def __repr__(self):
        # python >= 3.6
        return f'Cell(state={self.state}, row={self.row}, col={self.col})'
        # python < 3.6
        # return 'Cell(state={0.state}, row={0.row}, col={0.col})'.format(self)
同时,您也可以用同样的方法实现uu str u?

您必须在Cell类中实现u repr u。