类中的列表似乎是静态的?

时间:2018-01-08 23:29:47

标签: python list class static construction

在Python类中,我编写了以下方法:

class user():
    address_list = []

    def __init__(self):
        self.address_list.append("a")

当我用它时:

ur1 = user()
ur1.address_list.append("b")
ur2 = user()
ur2.address_list.append("c")
ur2.address_list.append("d")

print("user1's address: ", len(ur1.address_list))
print("user2's address: ", len(ur2.address_list))

输出结果为:

user1's address: 5
user2's address: 5

但我的期望是:

user1's address: 2   #a, b
user2's address: 3   #a,c,d

我的代码出了什么问题?

0 个答案:

没有答案