2013-09-05 157 views
6

我有一个小问题,我不明白。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 
+0

这个问题是不是* *严格相关的可变默认值,但大约是*创建*默认值时。 @tomek记住,每个函数都在'__defaults__'属性中保存了默认值的一个**元组**。但是,这是什么意思?那么,因为'tuple's是不可变的函数,*每次调用时都不能创建一个默认值,因此默认值在函数* definition *中只创建* once *。 尝试用'def func():print(“called”)''函数来改变'someObject',看看这个函数何时被调用。 – Bakuriu

+0

这是一个很好的问题。当我来自C++时,它肯定让我感到困惑 - 这些函数是在函数执行时评估的第二类对象,而不是函数定义。 – Shashank

回答

2

这是因为你指定默认的参数作为可变对象。

在python中,函数是一个对象,它在被定义时被评估,所以当你输入def appendList(self, new = someClass())时,你定义了new作为该函数的成员对象,并且它在执行时不会被重新评估。

看到“Least Astonishment” in Python: The Mutable Default Argument

+0

谢谢,现在我明白发生了什么事。这对我来说很直观,但我明白了。我忘了把功能当作对象。 –

相关问题