DEV Community

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

Posted on

Bytearray in Python (4)

Buy Me a Coffee

*Memo:

A bytearray can be changed by indexing, slicing and a del statement as shown below:

*Memo:

  • An iterable(int) and bytes-like object must be assigned to a sliced variable.
  • A del statement can remove zero or more bytes from a bytearray by indexing and slicing and can remove one or more variables themselves.
v1 = bytearray(b'ABCDEFGH')

v2 = v1[:]
v2 = v1[-100:100]
v2[-100:100] = v1[:]

v2[2:6] = [ord('0'), ord('1'), ord('2'), ord('3'), ord('4'), ord('5')]
# v2[-6:-2] = [ord('0'), ord('1'), ord('2'), ord('3'), ord('4'), ord('5')]
# v2[2:6] = b'012345'
# v2[-6:-2] = b'012345'
# v2[2:6] = bytearray(b'012345')
# v2[-6:-2] = bytearray(b'012345')

print(v2)
# bytearray(b'AB012345GH')

v2 = v1[:]

v2[2:6:2] = [ord('0'), ord('1')]
# v2[-6:-2:2] = [ord('0'), ord('1')]
# v2[2:6:2] = b'01'
# v2[-6:-2:2] = b'01'
# v2[2:6:2] = bytearray(b'01')
# v2[-6:-2:2] = bytearray(b'01')

print(v2)
# bytearray(b'AB0D1FGH')

v2 = v1[:]

v2[2:2] = []
# v2[-6:-6] = []
# v2[2:2] = b''
# v2[-6:-6] = b''
# v2[2:2] = bytearray(b'')
# v2[-6:-6] = bytearray(b'')

print(v2)
# bytearray(b'ABCDEFGH')

v2 = v1[:]

v2[2:6] = ord('0')
v2[-6:-2] = ord('0')
# TypeError: can assign only bytes, buffers, or iterables of ints
# in range(0, 256)
Enter fullscreen mode Exit fullscreen mode
v1 = bytearray(b'ABCDEFGH')

v2 = v1[:]

del v2[:], v2[-100:100]

print(v2)
# bytearray(b'')

v2 = v1[:]

del v2[2:6]
# del v2[-6:-2]

print(v2)
# bytearray(b'ABGH')

v2 = v1[:]

del v2[2:6:2]
# del v2[-6:-2:2]

print(v2)
# bytearray(b'ABDFGH')

v2 = v1[:]

del v2[2:2]
# del v2[-6:-6]

print(v1)
# bytearray(b'ABCDEFGH')

del v1, v2

print(v1)
# NameError: name 'v1' is not defined

print(v2)
# NameError: name 'v2' is not defined
Enter fullscreen mode Exit fullscreen mode

A bytearray can be continuously used through multiple variables as shown below:

v1 = v2 = v3 = bytearray(b'abcde') # Equivalent
                                   # v1 = bytearray(b'abcde')
v1[0] = ord(b'X')                  # v2 = v1
v2[3:5] = [ord(b'Y'), ord(b'Z')]   # v3 = v2
del v3[1:3]

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

A bytearray can be shallow-copied but cannot be deep-copied as shown below:

<Shallow & Deep copy>:

*Memo:

  • v1 and v2 refer to different bytearrays and each same byte.
  • is keyword can check if v1 and v2 refer to the same bytearray and each same byte.
  • bytearray.copy(), copy.copy(), bytearray() and slicing can shallow-copy a bytearray:
    • bytearray.copy() has no arguments.
  • copy.deepcopy() cannot deep-copy but can shallow-copy a bytearray.
import copy

v1 = bytearray(b'abcde')
v2 = v1.copy()
v2 = copy.copy(v1)
v2 = bytearray(v1)
v2 = v1[:]
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)