DEV Community

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

Posted on

Shallow copy & Deep copy in Python (11)

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 an string.
  • My post explains the shallow and deep copy of a bytes.
  • My post explains a bytes.

Different bytearrays are referred to if copied.


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

*Memo:

  • For a 2D bytearray, only shallow copy is possible.
  • A bytearray has 2 demesions(D).

<Assignment>:

*Memo:

  • v1 and v2 refer to the same shallow and deep bytearray.
  • is keyword can check if v1 and v2 refer to the same bytearray.

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

     # Shallow bytearray
#    ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓ 
v1 = bytearray(b'abcde')
v2 = v1        # ↑↑↑↑↑ Deep bytearray(Each byte)

print(v1) # bytearray(b'abcde')
print(v2) # bytearray(b'abcde')

print(v1 is v2, v1[2] is v2[2])
# True True True True

v2[1] = ord('X')
v2[3] = ord('Y')

print(v1) # bytearray(b'aXcYe')
print(v2) # bytearray(b'aXcYe')
Enter fullscreen mode Exit fullscreen mode

<Shallow copy>:

*Memo:

  • v1 and v2 refer to different shallow bytearrays.
  • v1 and v2 refer to the same deep bytearray.

bytearray.copy() shallow-copies the 2D bytearray as shown below:

v1 = bytearray(b'abcde')
v2 = v1.copy()

print(v1) # bytearray(b'abcde')
print(v2) # bytearray(b'abcde')

print(v1 is v2, v1[2] is v2[2])
# False True

v2[1]= ord('X')
v2[3]= ord('Y')
          #              ↓ ↓
print(v1) # bytearray(b'abcde')
print(v2) # bytearray(b'aXcYe')
          #              ↑ ↑
Enter fullscreen mode Exit fullscreen mode

copy.copy() shallow-copies a 2D bytearray as shown below:

import copy

v1 = bytearray(b'abcde')
v2 = copy.copy(v1)

print(v1) # bytearray(b'abcde')
print(v2) # bytearray(b'abcde')

print(v1 is v2, v1[2] is v2[2])
# False True

v2[1]= ord('X')
v2[3]= ord('Y')
          #              ↓ ↓
print(v1) # bytearray(b'abcde')
print(v2) # bytearray(b'aXcYe')
          #              ↑ ↑
Enter fullscreen mode Exit fullscreen mode

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

v1 = bytearray(b'abcde')
v2 = v1[:]

print(v1) # bytearray(b'abcde')
print(v2) # bytearray(b'abcde')

print(v1 is v2, v1[2] is v2[2])
# False True

v2[1]= ord('X')
v2[3]= ord('Y')
          #              ↓ ↓
print(v1) # bytearray(b'abcde')
print(v2) # bytearray(b'aXcYe')
          #              ↑ ↑
Enter fullscreen mode Exit fullscreen mode

bytearray() shallow-copies a 2D bytearray as shown below:

v1 = bytearray(b'abcde')
v2 = bytearray(v1)

print(v1) # bytearray(b'abcde')
print(v2) # bytearray(b'abcde')

print(v1 is v2, v1[2] is v2[2])
# False True

v2[1]= ord('X')
v2[3]= ord('Y')
          #              ↓ ↓
print(v1) # bytearray(b'abcde')
print(v2) # bytearray(b'aXcYe')
          #              ↑ ↑
Enter fullscreen mode Exit fullscreen mode

<Deep copy>:

*Memo:

  • v1 and v2 refer to different shallow bytearrays.
  • v1 and v2 refer to the same deep bytearray.

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

import copy

v1 = bytearray(b'abcde')
v2 = copy.deepcopy(v1)

print(v1) # bytearray(b'abcde')
print(v2) # bytearray(b'abcde')

print(v1 is v2, v1[2] is v2[2])
# False True

v2[1]= ord('X')
v2[3]= ord('Y')
          #              ↓ ↓
print(v1) # bytearray(b'abcde')
print(v2) # bytearray(b'aXcYe')
          #              ↑ ↑
Enter fullscreen mode Exit fullscreen mode

Top comments (0)