Python变量赋值,浅拷贝,深拷贝区别

从C说起

在C语言里,普通变量进行赋值,赋的是值,数组赋值,赋的是地址
eg

int a = 10;
int b = a;
==>a=b=10
a = 20;
==>a=20≠b=10
char c[10] = "apple";
char *d = c;
==>c=d->"apple"
c[0] = 'b';
==>c=d=>"bpple"

在Python中,分为可变变量和不可变变量,传递的都是地址,但是,可变变量的修改会对原始数据造成修改,不可变变量则不会。

  • 不可变变量(互不关心)
>>> a = 1
>>> b = a
>>> id(a)
140551545594848
>>> id(b)
140551545594848
>>> a = 2
>>> a
2
>>> b
1
>>> id(a)
140551545594880
>>> id(b)
140551545594848
  • 可变变量(祸福相依)
>>> a = ['1','2']
>>> b = a
>>> b == a
True
>>> a[0] = 0
>>> a
[0, '2']
>>> b
[0, '2']
>>> id(a) == id(b)
True

使用‘=’赋值

看一段代码

def fuzhi():
    a = [1,['s','z']]
    b = a
    print('a=%s,id(a)=%d,id(a[i])={%s}'%(a,id(a),[id(i) for i in a]))
    print('b=%s,id(b)=%d,id(b[i])={%s}'%(b,id(b),[id(i) for i in b]))
    a[0] = 2
    a[1].append('x')
    print('do some change on a...')
    print('a=%s,id(a)=%d,id(a[i])={%s}' % (a, id(a), [id(i) for i in a]))
    print('b=%s,id(b)=%d,id(b[i])={%s}' % (b, id(b), [id(i) for i in b]))

结果

a=[1, ['s', 'z']],id(a)=139889808571848,id(a[i])={[139889944073184, 139889808571912]}
b=[1, ['s', 'z']],id(b)=139889808571848,id(b[i])={[139889944073184, 139889808571912]}
do some change on a...
a=[2, ['s', 'z', 'x']],id(a)=139889808571848,id(a[i])={[139889944073216, 139889808571912]}
b=[2, ['s', 'z', 'x']],id(b)=139889808571848,id(b[i])={[139889944073216, 139889808571912]}

得到结论:

  • a就是b
  • 对a的修改会作用到b的身上
  • 不可变变量值修改会改变id
  • 可变变量的修改不会改变id

copy.copy

亦称浅拷贝
见代码:

def ShallowCopy():
    a = [1,['s','z']]
    b = copy.copy(a)
    print('a=%s,id(a)=%d,id(a[i])={%s}' % (a, id(a), [id(i) for i in a]))
    print('b=%s,id(b)=%d,id(b[i])={%s}' % (b, id(b), [id(i) for i in b]))
    a[0] = 2
    a[1].append('x')
    print('do some change on a...')
    print('a=%s,id(a)=%d,id(a[i])={%s}' % (a, id(a), [id(i) for i in a]))
    print('b=%s,id(b)=%d,id(b[i])={%s}' % (b, id(b), [id(i) for i in b]))

结果:

a=[1, ['s', 'z']],id(a)=139769512091080,id(a[i])={[139769647592416, 139769512091144]}
b=[1, ['s', 'z']],id(b)=139769512090952,id(b[i])={[139769647592416, 139769512091144]}
do some change on a...
a=[2, ['s', 'z', 'x']],id(a)=139769512091080,id(a[i])={[139769647592448, 139769512091144]}
b=[1, ['s', 'z', 'x']],id(b)=139769512090952,id(b[i])={[139769647592416, 139769512091144]}

得到结论:

  • a不是b(id改变)
  • 但copy.copy后拷贝使用原始变量内成员的指向(仅仅copy了可变变量的指向)
  • 对一方的修改,若是修改可变变量,会波及另一方;否则不会波及

copy.deepcopy

def DeepCopy():
    a = [1, ['s', 'z']]
    b = copy.deepcopy(a)
    print('a=%s,id(a)=%d,id(a[i])={%s}' % (a, id(a), [id(i) for i in a]))
    print('b=%s,id(b)=%d,id(b[i])={%s}' % (b, id(b), [id(i) for i in b]))
    a[0] = 2
    a[1].append('x')
    print('do some change on a...')
    print('a=%s,id(a)=%d,id(a[i])={%s}' % (a, id(a), [id(i) for i in a]))
    print('b=%s,id(b)=%d,id(b[i])={%s}' % (b, id(b), [id(i) for i in b]))
a=[1, ['s', 'z']],id(a)=139764479819208,id(a[i])={[139764615320544, 139764479819272]}
b=[1, ['s', 'z']],id(b)=139764479819080,id(b[i])={[139764615320544, 139764479819016]}
do some change on a...
a=[2, ['s', 'z', 'x']],id(a)=139764479819208,id(a[i])={[139764615320576, 139764479819272]}
b=[1, ['s', 'z']],id(b)=139764479819080,id(b[i])={[139764615320544, 139764479819016]
  • a!=b
  • 其中的可变变量也不是原来的了(仅仅值一样)
  • 无论是什么修改都不会影响另一方,即a,b已经相互独立

你可能感兴趣的:(python)