如何停止访问类变量而是访问实例变量?

问题描述

给定以下代码:

class GridSpace:
    space = "e"

class GridRow:  
    space1 = GridSpace()
    space2 = GridSpace()
    space3 = GridSpace()
    space4 = GridSpace()
    space5 = GridSpace()
    space6 = GridSpace()
    space7 = GridSpace()
    space8 = GridSpace()
    space9 = GridSpace()
    space10 = GridSpace()
    spaceList = [space1,space2,space3,space4,space5,space6,space7,space8,space9,space10]

class Grid:    
    gridRow1 = GridRow()
    gridRow2 = GridRow()
    gridRow3 = GridRow()
    gridRow4 = GridRow()
    gridRow5 = GridRow()
    gridRow6 = GridRow()
    gridRow7 = GridRow()
    gridRow8 = GridRow()
    gridRow9 = GridRow()
    gridRow10 = GridRow()
    rowList = [gridRow1,gridRow2,gridRow3,gridRow4,gridRow5,gridRow6,gridRow7,gridRow8,gridRow9,gridRow10]

grid = Grid()

grid.rowList[0].spaceList[0].space = "s"

for x in grid.rowList:
    rowWord = ""
    for y in x.spaceList:
        rowWord = rowWord + y.space + " "
    print(rowWord)

我想输出一个 e 字符的 10x10 网格,除了左上角的应该是 s 字符的字符。相反,我正在更改每个行列表的第一个元素的类变量,而不是仅更改第一行列表的第一个元素的实例变量。如何使它只将第一个字符更改为 s,而将其他 99 个字符保留为 e?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)