Open ropering opened 2 years ago
리스트안에 리스트 mutable객체 안에 mutable객체인 경우 문제가 됩니다.
id(a)
id(b)
id(a[0])
id(b[0])
>>> 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]
b[1]
>>> a[1].append(5) >>> a [[8, 9], [3, 4, 5]] >>> b [[1, 2], [3, 4, 5]] >>> id(a[1]) 4396389896 >>> id(b[1]) 4396389896
[[python] 파이썬 얕은복사, 깊은복사 (copy, deepcopy, [:], =) 총 정리](https://blockdmask.tistory.com/576)
[점프 투 파이썬](https://wikidocs.net/16038)
얕은 복사(shallow copy)
깊은 복사 (Deep copy)
몰랐던 것
리스트안에 리스트 mutable객체 안에 mutable객체인 경우 문제가 됩니다.
id(a)
값과id(b)
값은 다르게 되었지만, 그 내부의 객체id(a[0])
과id(b[0])
은 같은 주소를 바라보고 있습니다.a[1]
에 값을 변경하면b[1]
도 따라 변경됩니다.References
[[python] 파이썬 얕은복사, 깊은복사 (copy, deepcopy, [:], =) 총 정리](https://blockdmask.tistory.com/576)
[점프 투 파이썬](https://wikidocs.net/16038)