0

The answer here says:

Shallow copies duplicate as little as possible. A shallow copy of a collection is a copy of the collection structure, not the elements. With a shallow copy, two collections now share the individual elements.

Deep copies duplicate everything. A deep copy of a collection is two collections with all of the elements in the original collection duplicated.

consider this following piece in python 2.7.13

import copy
a = [1234567,12345678,(123456789,1234567890)]
a_d = copy.deepcopy(a)
a[1] is a_d[1] #True
a[2] is a_d[2] #True
a[2] is a_d[2][1] #True

Why aren't the individual objects duplicated here?

Community
  • 1
  • 1
figs_and_nuts
  • 4,870
  • 2
  • 31
  • 56
  • 3
    Because integers and tuples are immutable. There's no need to make copies of immutable objects because they can be shared safely. This is discussed in [the second answer](http://stackoverflow.com/a/17246743/4014959) of the linked question. – PM 2Ring Apr 21 '17 at 13:46
  • 1
    Tuples are also immutable, so there's no need to duplicate them as well. – ForceBru Apr 21 '17 at 13:47
  • For further info on the details of what `copy.deepcopy` does, I suggest you have a look at its [source code](https://github.com/python-git/python/blob/master/Lib/copy.py). – PM 2Ring Apr 21 '17 at 13:54

0 Answers0