我有一个小问题,我不明白。Python - 为什么它不创建对象的新实例?
我有一个方法:
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
这个问题是不是* *严格相关的可变默认值,但大约是*创建*默认值时。 @tomek记住,每个函数都在'__defaults__'属性中保存了默认值的一个**元组**。但是,这是什么意思?那么,因为'tuple's是不可变的函数,*每次调用时都不能创建一个默认值,因此默认值在函数* definition *中只创建* once *。 尝试用'def func():print(“called”)''函数来改变'someObject',看看这个函数何时被调用。 – Bakuriu
这是一个很好的问题。当我来自C++时,它肯定让我感到困惑 - 这些函数是在函数执行时评估的第二类对象,而不是函数定义。 – Shashank