DEV Community

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

Posted on • Edited on

Bytes in Python (2)

Buy Me a Coffee

*Memo:

A bytes can be read by indexing or slicing as shown below:

*Memo:

  • Indexing can be done with one or more [index].
  • Slicing can be done with one or more [start:end:step]:
    • start(Optional-Default:The index of the 1st element):
      • It's a start index(inclusive).
    • end(Optional-Default:The index of the last element + 1):
      • It's an end index(exclusive).
    • step(Optional-Default:1):
      • It's the interval of indices.
      • It cannot be zero.
    • The [] with at least one : is slicing.
v = b'ABCDEFGH'

print(v)
# b'ABCDEFGH'

print(v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7])
print(v[-8], v[-7], v[-6], v[-5], v[-4], v[-3], v[-2], v[-1])
# 65 66 67 68 69 70 71 72
Enter fullscreen mode Exit fullscreen mode
v = b'ABCDEFGH'

print(v[:])
print(v[::])
# b'ABCDEFGH'

print(v[::2])
# b'ACEG'

print(v[::-2])
# b'HFDB'

print(v[2:])
print(v[-6:])
print(v[2::])
print(v[-6::])
# b'CDEFGH'

print(v[2::2])
print(v[-6::2])
# b'CEG'

print(v[2::-2])
print(v[-6::-2])
# b'CA'

print(v[:6])
print(v[:-2])
print(v[:6:])
print(v[:-2:])
# b'ABCDEF'

print(v[:6:2])
print(v[:-2:2])
# b'ACE'

print(v[:6:-2])
print(v[:-2:-2])
# b'H'

print(v[2:6])
print(v[-6:-2])
print(v[2:6:])
print(v[-6:-2:])
# b'CDEF'

print(v[2:6:2])
print(v[-6:-2:2])
# b'CE'

print(v[2:6:-2])
print(v[-6:-2:-2])
# b''
Enter fullscreen mode Exit fullscreen mode

A bytes cannot be changed by indexing or slicing as shown below:

*Memo:

  • A del statement can still be used to remove one or more variables themselves.
v = b'abcdef'
v = bytes(b'abcdef')

v[0] = b'X'
v[2:6] = [b'Y', b'Z']
# TypeError: 'bytes' object does not support item assignment
Enter fullscreen mode Exit fullscreen mode
v = b'abcdef'
v = bytes(b'abcdef')

del v[0], v[3:5]
# TypeError: 'bytes' object does not support item deletion
Enter fullscreen mode Exit fullscreen mode
v = b'abcdef'
v = bytes(b'abcdef')

del v

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

If you really want to change a bytes, use bytearray(), ord() and bytes() as shown below.

v = b'abcdef'
v = bytes(b'abcdef')

v = bytearray(v)

v[0] = ord(b'X')
v[2:6] = [ord(b'Y'), ord(b'Z')]

v = bytes(v)

print(v)
# b'XbYZ'
Enter fullscreen mode Exit fullscreen mode
v = b'abcdef'
v = bytes(b'abcdef')

v = bytearray(v)

del v[0], v[3:5]

v = bytes(v)

print(v)
# bcd
Enter fullscreen mode Exit fullscreen mode

In addition, if you really want to change a bytes, use list(), chr(), str.encode() or int.to_bytes() and bytes.join() as shown below.

v = b'abcdef'
v = bytes(b'abcdef')

v = [chr(x).encode() for x in list(v)]
# v = [x.to_bytes() for x in v]

v[0] = b'X'
v[2:6] = [b'Y', b'Z']

v = b''.join(v)

print(v)
# b'XbYZ'
Enter fullscreen mode Exit fullscreen mode
v = b'abcdef'
v = bytes(b'abcdef')

v = [chr(x).encode() for x in list(v)]
# v = [x.to_bytes() for x in v]

del v[0], v[3:5]

v = b''.join(v)

print(v)
# b'bcd'
Enter fullscreen mode Exit fullscreen mode

The variables v1 and v2 always refer to the same bytes because a bytes cannot be copied as shown below:

*Memo:

  • is keyword or is and not keyword can check if v1 and v2 refer or don't refer to the same bytes respectively.
  • copy.copy() and slicing do shallow copy.
  • bytes() doesn't do shallow copy.
  • copy.deepcopy() does deep copy.
  • copy.deepcopy() should be used because it's safe, doing copy deeply while copy.copy() and slicing aren't safe, doing copy shallowly.
import copy

v1 = b'abcde'

v2 = v1 # v2 refers to the same bytes as v1.

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

print(v1 is v2, v1 is not v2)
# True False

# v2 refers to the same bytes as v1.
v2 = copy.copy(v1)
v2 = copy.deepcopy(v1)
v2 = bytes(v1)
v2 = v1[:]

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

print(v1 is v2, v1 is not v2)
# True False
Enter fullscreen mode Exit fullscreen mode

Top comments (0)