如何停止访问 class 变量,转而访问实例变量?
How do I stop accessing class variables and instead access an instance variable?
给定以下代码:
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 字符。相反发生的是我正在更改每个行列表的第一个元素的 class 变量,而不是仅第一行列表的第一个元素的实例变量。如何让它只将第一个字符更改为 s,而将其他 99 个字符保留为 e?
如果您希望变量成为实例变量,您必须这样定义它们(例如在 __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: 我使用了列表理解来简化代码。
出现这种情况是因为下面的语句是True
grid = Grid()
print(Grid.rowList[0].spaceList[0] is Grid.rowList[1].spaceList[0]) # outputs True
在这里您可以看到第一行的 spaceList[0]
与第二行的 spaceList[0]
完全相同(它们指的是同一个对象,当一个对象发生变化时,另一个对象也发生变化) .
正如您已经猜到的那样,您需要将它们转换为 instance 变量。但是你必须做这个 2 级深度,这意味着你必须将它应用于 GridSpace
和 GridRow
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
给定以下代码:
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 字符。相反发生的是我正在更改每个行列表的第一个元素的 class 变量,而不是仅第一行列表的第一个元素的实例变量。如何让它只将第一个字符更改为 s,而将其他 99 个字符保留为 e?
如果您希望变量成为实例变量,您必须这样定义它们(例如在 __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: 我使用了列表理解来简化代码。
出现这种情况是因为下面的语句是True
grid = Grid()
print(Grid.rowList[0].spaceList[0] is Grid.rowList[1].spaceList[0]) # outputs True
在这里您可以看到第一行的 spaceList[0]
与第二行的 spaceList[0]
完全相同(它们指的是同一个对象,当一个对象发生变化时,另一个对象也发生变化) .
正如您已经猜到的那样,您需要将它们转换为 instance 变量。但是你必须做这个 2 级深度,这意味着你必须将它应用于 GridSpace
和 GridRow
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