【问题标题】:putting information from a definiton in a class method __str__将定义中的信息放入类方法 __str__
【发布时间】:2016-12-13 01:09:26
【问题描述】:

我正在尝试将此函数中的信息获取到类方法 str

def display(self)`:
        '''
        to see the grid and check to see if everything is working
        '''

    # header
    print("  0   1")
    # grid
    print('-'*8)
    for i, row in enumerate(self.grid):

        print('0|'.format(i), end='') #prints the 0 and 1 on the left

        for cell in row: 
            print('{:2s}|'.format(str(cell)), end="")

        print()
        print('-'*8)

但我希望此信息显示在类的 str 函数中

def __str__(self):
    '''
    returns a string representation of a CarpetSea, i.e. display the organized contents of each cell. 
    Rows and columns should be labeled with indices.

    Example (see "Example Run" in the PA8 specs for more):
      0  1  
    --------
    0|M |  |
    --------
    1|  |* |
    --------

    Note: call Cell's __str__() to get a string representation of each Cell in grid
    i.e. "M " for Cell at (0,0) in the example above
    '''
    return 

这是我用于创建网格的一些代码

def __init__(self, N):
    '''

    '''
    self.N = N
    self.grid = []
    for i in range(self.N):
        row = []
        for j in range(self.N):
            cell = Cell(i, j)
            row.append(cell)
        self.grid.append(row)


    self.available_fish = ["Salmon", "Marlin", "Tuna", "Halibut"]

这是我更新的 str 函数,但它给出了序列错误

def __str__(self):
    '''
    returns a string representation of a CarpetSea, i.e. display the organized contents of each cell. 
    Rows and columns should be labeled with indices.

    Example (see "Example Run" in the PA8 specs for more):
      0  1  
    --------
    0|M |  |
    --------
    1|  |* |
    --------

    Note: call Cell's __str__() to get a string representation of each Cell in grid
    i.e. "M " for Cell at (0,0) in the example above
    '''        
    self.grid.append("1     0")
    for _ in range(self.N):
        self.grid.append("-" * 8)

    return "\n".join(self.grid)

【问题讨论】:

    标签: python list class


    【解决方案1】:

    制作行列表。加入他们的新行。

    class Grid:
      def __init__(self, w, h):
        self.w = w
        self.h = h
    
      def __str__(self):
        lines = []
        lines.append("header")
        for _ in range(self.h):
            lines.append('-' * self.w)
    
        return "\n".join(lines)
    
    g = Grid(4, 5)
    print(g)
    

    输出

    header
    ----
    ----
    ----
    ----
    ----
    

    换句话说,print 应该是 lines.append()。这是一个练习,让您了解如何处理end='' 的转换。

    【讨论】:

    • 除此之外,我强烈建议将 display 更改为只执行 print(self) 一次 __str__ 执行您想要的操作。将相同的代码实施两次,只有细微的差别,几乎没有什么好处,而且存在重大的维护问题。
    • 我添加了已经创建网格的初始化函数。您如何建议使用我已经拥有的代码制作列表。我可能会在str 工作后立即删除display 函数
    • self.grid.append("1 0") for _ in range(self.N): self.grid.append("-" * 8) return "\n".join(self.grid) 我试过这段代码,但我得到一个序列错误@ShadowRanger
    • 该代码看起来不错;我没有看到任何可能导致该错误的东西
    • 它给了我一个序列错误,我在我的代码@cricket_007 中添加了我的__str__ 功能
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-14
    • 2013-12-09
    相关资源
    最近更新 更多