我试图理解Python中的异化行为.
我有下一个python代码:
class IntContainer:
listOfInts = []
def __init__(self, initListOfInts):
for i in initListOfInts:
self.listOfInts.append(i)
def printInts(self):
print self.listOfInts
if __name__ == "__main__":
intsGroup1 = [1,2,3,4]
intsGroup2 = [4,5,6,7]
intsGroups = [intsGroup1,intsGroup2]
intsContainers = []
for ig in intsGroups:
newIntContainer = IntContainer(ig)
intsContainers.append(newIntContainer)
for ic in intsContainers:
print ic.listOfInts
Run Code Online (Sandbox Code Playgroud)
我希望得到类似的东西:
[1, 2, 3, 4]
[4, 5, 6, 7]
Run Code Online (Sandbox Code Playgroud)
但我得到:
[1, 2, 3, 4, 4, 5, 6, 7]
[1, 2, 3, 4, 4, 5, 6, 7]
Run Code Online (Sandbox Code Playgroud)
我检查了下一个问题:
还有很多Python参考,但我无法理解发生了什么.我认为与newIntContainer标识符重用度有关,但我不深刻理解. …