我正在执行以下Python代码:
class Pet :
kind = 'dog' # class variable shared by all instances
tricks = []
def __init__(self, name) :
self.name = name # instance variable unique to each instance
def changePet(self, newPet) :
self.kind = newPet
def addTricks(self, tricks) :
self.tricks.append(tricks)
pet1 = Pet('mypet')
pet2 = Pet('yourpet')
print 'pet1 kind ::: ', pet1.kind;print 'pet2 kind ::: ', pet2.kind
print 'pet1 name ::: ', pet1.name;print 'pet2 name ::: ', pet2.name
Pet.kind = 'cat'
print 'changed Pet.kind to cat'
print 'pet1 kind ::: ', pet1.kind;print 'pet2 kind ::: ', pet2.kind
#changing pet#1 kind does not change pet#2 kind
pet1.changePet('parrot')
print 'changed pet1.kind to parrot'
print 'pet1 kind ::: ', pet1.kind;print 'pet2 kind ::: ', pet2.kind
pet1.addTricks('imitate')
pet2.addTricks('roll over')
print 'pet1 tricks ::: ', pet1.tricks;print 'pet2 tricks ::: ', pet2.tricks
print Pet.__dict__
print pet1.__dict__
print pet2.__dict__
输出是根据我在互联网上找到的解释。输出如下
pet1 kind ::: dog
pet2 kind ::: dog
pet1 name ::: mypet
pet2 name ::: yourpet
changed Pet.kind to cat
pet1 kind ::: cat
pet2 kind ::: cat
changed pet1.kind to parrot
pet1 kind ::: parrot
pet2 kind ::: cat
pet1 tricks ::: ['imitate', 'roll over']
pet2 tricks ::: ['imitate', 'roll over']
{'__module__': '__main__', 'tricks': ['imitate', 'roll over'], 'kind': 'cat', 'addTricks': <function addTricks at 0xb71fa6bc>, 'changePet': <function changePet at 0xb71fa33c>, '__doc__': None, '__init__': <function __init__ at 0xb71fa144>}
{'kind': 'parrot', 'name': 'mypet'}
{'name': 'yourpet'}
现在我的查询是为什么可变类对象的处理方式与非可变类对象不同
答案 0 :(得分:1)
我不确定他们对待的方式不同。您只是对它们执行不同的操作。
对于changePet
,您要将之前未定义的self.kind
分配给值。现在,当python试图找到pet1.kind
时,它会立即找到它。查找pet2.kind
时,它找不到它。但是,它确实找到了Pet.kind
,因此它会返回。
如果是addTricks
,您正试图改变self.tricks
。由于这不存在,因此您可以参考Pet.tricks
。因此,当您在append()
上致电self.tricks
时,您可以有效地致电Pet.tricks.append()
,而不是self.tricks.append()
。
为了澄清事情,请:
def changePet(self, newPet) :
self.kind = newPet
def addTricks(self, tricks) :
self.tricks.append(tricks)
实际上等同于:
def changePet(self, newPet) :
self.kind = newPet
def addTricks(self, tricks) :
Pet.tricks.append(tricks)
为了证明python不是以不可变的方式处理mutable,我们需要更改你的方法,以便它们执行类似的操作:
def changePet(self, newPet) :
self.kind = newPet
def addTricks(self, tricks):
# assign self.tricks to a new value where we previously only mutated it!
# note: list(self.tricks) returns a copy
self.tricks = list(self.tricks)
self.tricks.append(tricks)
现在,当我们再次运行您的代码时,我们得到以下输出:
pet1 kind ::: dog
pet2 kind ::: dog
pet1 name ::: mypet
pet2 name ::: yourpet
changed Pet.kind to cat
pet1 kind ::: cat
pet2 kind ::: cat
changed pet1.kind to parrot
pet1 kind ::: parrot
pet2 kind ::: cat
pet1 tricks ::: ['imitate']
pet2 tricks ::: ['roll over']
{'__module__': '__main__', 'tricks': [], 'kind': 'cat', 'addTricks': <function addTricks at 0x02A33C30>, 'changePet': <function changePet at 0x02A33BF0>, '__doc__': None, '__init__': <function __init__ at 0x02A33BB0>}
{'tricks': ['imitate'], 'kind': 'parrot', 'name': 'mypet'}
{'tricks': ['roll over'], 'name': 'yourpet'}