DEV Community

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

Posted on • Edited on

Bytearray shallow & deep copy in Python

Buy Me a Coffee

*Memo for shallow and deep copy:

*Memo for others:

  • My post explains a bytearray (1).

Different bytearrays are referred to, only shallow-copied.


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

*Memo:

  • A 2D bytearray can be shallow-copied but cannot be deep-copied.
  • A bytearray 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 object and (optional) original lower levels' objects, keeping the same values as before.
    • A shallow copy is to create the one or more references to the new top level object and (optional) original lower levels' objects, keeping the same values as before.
    • A deep copy is to create the two or more references to the new top level object and the new lower levels' objects which you desire but at least the new 2nd level objects, keeping the same values as before:
      • A deep copy is the multiple recursions of a shallow copy so a deep copy can be done with multiple shallow copies.
    • Basically, immutable(hashable) objects aren't copied to save memory like str, bytes, int, float, complex, bool and tuple.

<Assignment>:

*Memo:

  • v1 and v2 refer to the same bytearray and each same byte.
  • is keyword can check if v1 and v2 refer to the same bytearray and each same byte.

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

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

print(v1, v1[2]) # bytearray(b'abcde') 99
print(v2, v2[2]) # bytearray(b'abcde') 99

print(v1 is v2, v1[2] is v2[2])
# 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 bytearrays and each same byte.

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

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

print(v1, v1[2]) # bytearray(b'abcde') 99
print(v2, v2[2]) # bytearray(b'abcde') 99

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() can shallow-copy a 2D bytearray as shown below:

import copy

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

print(v1, v1[2]) # bytearray(b'abcde') 99
print(v2, v2[2]) # bytearray(b'abcde') 99

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() can shallow-copy a 2D bytearray as shown below:

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

print(v1, v1[2]) # bytearray(b'abcde') 99
print(v2, v2[2]) # bytearray(b'abcde') 99

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 can shallow-copy the 2D bytearray as shown below:

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

print(v1, v1[2]) # bytearray(b'abcde') 99
print(v2, v2[2]) # bytearray(b'abcde') 99

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 bytearrays and each same byte.

copy.deepcopy() cannot deep-copy but can shallow-copy a 2D bytearray as shown below:

import copy

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

print(v1, v1[2]) # bytearray(b'abcde') 99
print(v2, v2[2]) # bytearray(b'abcde') 99

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)