ropering / Study

0 stars 0 forks source link

[TIL] 22.04.29 #41

Open ropering opened 2 years ago

ropering commented 2 years ago

얕은 복사(shallow copy)


깊은 복사 (Deep copy)


몰랐던 것


리스트안에 리스트 mutable객체 안에 mutable객체인 경우 문제가 됩니다.

>>> a = [[1,2], [3,4]]
>>> b = a[:]
>>> id(a)
4395624328
>>> id(b)
4396179592
>>> id(a[0])
4396116040
>>> id(b[0])
4396116040
>>> a = [[1,2], [3,4]]
>>> b = a[:] # 같은 주소값이 복사되는 게 아닌 전혀 다른 새로운 객체가 만들어진 후 그 객체의 주소값이 저장된다
>>> id(a)
4395624328
>>> id(b)
4396179592
>>> id(a[0])
4396116040
>>> id(b[0])
4396116040
>>> a[1].append(5)
>>> a
[[8, 9], [3, 4, 5]]
>>> b
[[1, 2], [3, 4, 5]]
>>> id(a[1])
4396389896
>>> id(b[1])
4396389896

References


[[python] 파이썬 얕은복사, 깊은복사 (copy, deepcopy, [:], =) 총 정리](https://blockdmask.tistory.com/576)

[점프 투 파이썬](https://wikidocs.net/16038)