I've written a class that has a list as a variable. I have a function that adds to that list and a function that outputs that list.
class MyClass:
myList = []
def addToList(self, myNumber):
self.myList.append(myNumber)
def outputList(self):
for someNumber in self.myList:
print someNumber
Now for some weird reason, if I declare two separate objects of the class:
ver1 = MyClass()
ver2 = MyClass()
and then call addToList on ver1:
ver1.addToList(3)
and then output ver2's list:
ver2.outputList()
I get 3 as output for version 2's list! What is happening?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…