我有一个小问题,我不明白。
我有一个方法:
def appendMethod(self, newInstance = someObject()):
self.someList.append(newInstace)
我调用这个方法没有属性:
object.appendMethod()
实际上,我在列表中附加了相同的 someObject 实例。
但是,如果我将其更改为:
def appendMethod(self):
newInstace = someObject()
self.someList.append(newInstance)
我每次都会得到该对象的新实例,有什么区别?
这是一个例子:
class someClass():
myVal = 0
class otherClass1():
someList = []
def appendList(self):
new = someClass()
self.someList.append(new)
class otherClass2():
someList = []
def appendList(self, new = someClass()):
self.someList.append(new)
newObject = otherClass1()
newObject.appendList()
newObject.appendList()
print newObject.someList[0] is newObject.someList[1]
>>>False
anotherObject = otherClass2()
anotherObject.appendList()
anotherObject.appendList()
print anotherObject.someList[0] is anotherObject.someList[1]
>>>True