DEV Community

Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito)

Posted on

Shallow copy & Deep copy in Python (12)

Buy Me a Coffee

*Memo:

  • My post explains the shallow and deep copy of a list.
  • My post explains the shallow and deep copy of a tuple.
  • My post explains the shallow and deep copy of the set with a frozenset.
  • My post explains the shallow and deep copy of the set with a tuple.
  • My post explains the shallow and deep copy of the set with an iterator.
  • My post explains the shallow and deep copy of a frozenset.
  • My post explains the shallow and deep copy of a dictionary.
  • My post explains the shallow and deep copy of an iterator.
  • My post explains the shallow and deep copy of a string.
  • My post explains the shallow and deep copy of a bytes.
  • My post explains the shallow and deep copy of a bytearray.
  • My post explains range().

Different ranges are referred to only if shallow-copied by slicing.


A 2D range is experimented, doing assignment and shallow and deep copy as shown below:

*Memo:

  • For a 2D range, only shallow copy is possible.
  • A range has 2 dimensions(D).
  • There are an assignment and 2 kinds of copies, shallow copy and deep copy:
    • An assignment is to create the one or more references to the original top-level of an object and (optional) each original nested level of an object, keeping the same values as before.
    • A shallow copy is to create the one or more references to the new top-level of an object and (optional) each original nested level of an object, keeping the same values as before.
    • A deep copy is to create the one or more references to the new top-level of an object and each new nested level of an object, keeping the same values as before.
    • Basically, immutable(hashable) objects aren't copied.

<Assignment>:

*Memo:

  • v1 and v2 refer to the same outer and inner range.
  • is keyword can check if v1 and v2 refer to the same range.

A 2D range is assigned to a variable without copied as shown below:

#  Outer range
#    ↓↓↓↓↓↓↓↓ 
v1 = range(5)
v2 = v1  # ↑ Inner range(Each element)

print(v1, *v1) # range(0, 5) 0 1 2 3 4
print(v2, *v2) # range(0, 5) 0 1 2 3 4

print(v1 is v2, v1[2] is v2[2])
# True True
Enter fullscreen mode Exit fullscreen mode

<Shallow copy>:

*Memo:

  • v1 and v2 refer to different outer ranges only by slicing.
  • v1 and v2 refer to the same inner range.

copy.copy() doesn't shallow-copy a 2D range as shown below:

import copy

v1 = range(5)
v2 = copy.copy(v1)

print(v1, *v1) # range(0, 5) 0 1 2 3 4
print(v2, *v2) # range(0, 5) 0 1 2 3 4

print(v1 is v2, v1[2] is v2[2])
# True True
Enter fullscreen mode Exit fullscreen mode

Slicing shallow-copies the 2D range as shown below:

v1 = range(5)
v2 = v1[:]

print(v1, *v1) # range(0, 5) 0 1 2 3 4
print(v2, *v2) # range(0, 5) 0 1 2 3 4

print(v1 is v2, v1[2] is v2[2])
# False True
Enter fullscreen mode Exit fullscreen mode

<Deep copy>:

*Memo:

  • v1 and v2 refer to the same outer and inner range.

copy.deepcopy() doesn't deep-copy a 2D range as shown below:

import copy

v1 = range(5)
v2 = copy.deepcopy(v1)

print(v1, *v1) # range(0, 5) 0 1 2 3 4
print(v2, *v2) # range(0, 5) 0 1 2 3 4

print(v1 is v2, v1[2] is v2[2])
# True True
Enter fullscreen mode Exit fullscreen mode

Top comments (0)