python类的奇怪行为

时间:2011-11-16 02:36:04

标签: python class constructor initialization

这是一个python类:

class TxdTest(object):
    def __init__(self, name = '', atrributes = []):
        self.name = name
        self.attributes = atrributes

然后我像这样使用它:

def main():
    for i in range(3):
        test = TxdTest()
        print test.name
        print test.attributes
        test.name = 'a'
        test.attributes.append(1)

那么,结果是什么?结果是:

[]

[1]

[1,1]

为什么课堂上的'self.attributes'仍然获得价值?

3 个答案:

答案 0 :(得分:4)

当将可变对象(如列表)传递给python中的函数或方法时,会在函数或方法的所有调用中使用单个对象。这意味着,无论何时调用函数或方法(在本例中为您的__init__方法),每次都会使用完全相同的列表,并保留以前进行的修改。

如果您希望将空列表作为默认列表,则应执行以下操作:

class TxdTest(object):
    def __init__(self, name = '', atrributes = None):
        self.name = name
        if attributes is None
             self.attributes = []
        else:
             self.attributes = attributes

有关其工作原理的详细说明,请参阅:“Least Astonishment” in Python: The Mutable Default Argument,如bgporter所述。

答案 1 :(得分:1)

简短回答:只有一个列表,因为它是在函数定义时分配的,而不是在调用它时。因此,该类的所有实例都使用相同的attributes列表。

与班级无关;每当您将可变对象用作函数参数列表中的默认值时,就会出现此问题。

答案 2 :(得分:0)

这是此处描述的“Python陷阱”之一:http://zephyrfalcon.org/labs/python_pitfalls.html

(你需要#2和#5。文章来自2003年,但它仍适用于现代Python版本。)

相关问题