【问题标题】:How do I stop accessing class variables and instead access an instance variable?如何停止访问类变量而访问实例变量?
【发布时间】:2021-04-21 15:17:08
【问题描述】:

给出以下代码:

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)

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

【问题讨论】:

  • 您可以使用 for 循环来创建 GridRow 和 GridSpace 对象的列表。
  • 创建了类变量...

标签: python class class-variables


【解决方案1】:

这是因为下面的语句是True

grid = Grid()
print(Grid.rowList[0].spaceList[0] is Grid.rowList[1].spaceList[0]) # outputs True

在这里您可以看到第一行的spaceList[0] 与第二行的spaceList[0] 完全相同(它们指的是同一个对象,当一个对象被更改时,另一个对象也是如此)。

正如您已经猜到的那样,您需要将这些转换为 instance 变量。但是您必须执行此 2 级深度,这意味着您必须将其应用于 GridSpaceGridRow

class GridSpace:
    def __init__(self):
        self.space = "e" # Now "e" is unique to every column

还有

class GridRow:  
    def __init__(self):
        self.spaceList = [GridSpace() for _ in range(10)] 

现在声明 print(Grid.rowList[0].spaceList[0] is Grid.rowList[1].spaceList[0]) 确实是 False 并且您的代码运行良好

s e e e e e e e e e 
e e e e e e e e e e 
e e e e e e e e e e 
e e e e e e e e e e 
e e e e e e e e e e 
e e e e e e e e e e 
e e e e e e e e e e 
e e e e e e e e e e 
e e e e e e e e e e 
e e e e e e e e e e 

【讨论】:

    【解决方案2】:

    如果您希望变量成为实例变量,则必须这样定义它们(例如,在 __init__ 函数中使用 self 参数):

    class GridSpace:
        def __init__(self):
            self.space = "e"
    
    class GridRow: 
        def __init__(self):
            self.spaceList = [GridSpace() for i in range(10)]
    
    class Grid:    
        def __init__(self):
            self.rowList = [GridRow() for i in range(10)]
    

    PS:我使用列表推导来简化代码。

    【讨论】:

      猜你喜欢
      • 2011-02-21
      • 1970-01-01
      • 1970-01-01
      • 2012-03-22
      • 1970-01-01
      • 1970-01-01
      • 2022-08-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多