本帖最后由 zlj19931010 于 2017-5-8 16:58 编辑
>>> a = 1
>>> b = a
>>> a += 1
>>> a
2
>>> b
1
>>> type(a)
<class 'int'>
>>> type(b)
<class 'int'>
>>> a.__repr__
<method-wrapper '__repr__' of int object at 0x0000000056B0A480>
>>> b.__repr__
<method-wrapper '__repr__' of int object at 0x0000000056B0A460>
Numeric Types 类型的赋值,也就是b = a, a和b两个是不同的对象了,python自己做了重新创建对象的操作
>>> a = []
>>> b = a
>>> type(a)
<class 'list'>
>>> type(b)
<class 'list'>
>>> a.__repr__
<method-wrapper '__repr__' of list object at 0x00000000030FC0C8>
>>> b.__repr__
<method-wrapper '__repr__' of list object at 0x00000000030FC0C8>
Sequence Types 类型的赋值,b = a,并没有创建新对象,两个指向一样的
所以:>>> a1 = a()
>>> a2 = a()
>>> a1.a
[]
>>> a1.a.__repr__
<method-wrapper '__repr__' of list object at 0x000000000016EE48>
>>> a2.a.__repr__
<method-wrapper '__repr__' of list object at 0x000000000016EE48>
>>>
每个实例对象指向的是同一个类变量
到这里,我也发现了,其实造成你这种现象主要是因为 值类型 和 引用类型 在赋值的时候采取的策略不同 |