Posts

Showing posts with the label Python copy module

Python advanced #002 shallow copy() and deepcopy() (2020)

Image
1. Introduction to copy module There are two copy method in the copy module, copy.copy() and copy.deepcopy(). Those two methods could cause ambiguity in some cases. So it is crutial that we understant it clearly. 1.1 copy.copy() There are two lists a and b: >>> a = [11,22] >>> b = [33,44] In python, the two lists are stored in different addresses in memory. The variable names "a" and "b" are just like labels attached to the lists and they are also stored in memory too and they know the addresses of those lists. Next, we calculate the addresses of those two lists by using built-in function id(). >>> id(a) 43440616 >>> id(b) 39008264 As you could see, these two have different addresses which means they occupy separate spaces in memory. Next, we create a list "c" out of those two lists "a" and "b": >>> c = [a, b] >>> id(c) 39007720 As you could see, "c" points to a diffrent ad