DEV Community

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

Posted on • Edited on

Bytes in Python (3)

Buy Me a Coffee

*Memo:

A bytes can be enlarged with * and a number as shown below:

v = b'ABCDE' * 3

print(v)
# b'ABCDEABCDEABCDE'
Enter fullscreen mode Exit fullscreen mode
v = b'01234' * 3

print(v)
# b'012340123401234'
Enter fullscreen mode Exit fullscreen mode
v = b'' * 3

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

A bytes object and other bytes objects can be concatenated with + as shown below:

v = b'ABC' + b'DE' + b'FGHI'

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

A bytes object and other bytes object cannot return:

  • all the bytes in them with '|' (Union: A ∪ B).
  • their common bytes with '&' (Intersection: A ∩ B).
  • the bytes in the bytes object which aren't in other bytes object with '-' (Difference: A - B).
  • the bytes in either the bytes object or other bytes object but not both with '^' (Symmetric Difference: A Δ B).
v = b'AE' | b'ACE'
# TypeError: unsupported operand type(s) for |: 'bytes' and 'bytes'
Enter fullscreen mode Exit fullscreen mode
v = b'ABCD' & b'ACE'
# TypeError: unsupported operand type(s) for &: 'bytes' and 'bytes'
Enter fullscreen mode Exit fullscreen mode
v = b'ABCD' - b'ACE'
# TypeError: unsupported operand type(s) for -: 'bytes' and 'bytes'
Enter fullscreen mode Exit fullscreen mode
v = b'ABCD' ^ b'ACE'
# TypeError: unsupported operand type(s) for ^: 'bytes' and 'bytes'
Enter fullscreen mode Exit fullscreen mode

A bytes can be iterated with a for statement as shown below:

for v in b'ABC':
    print(v)
# 65
# 66
# 67
Enter fullscreen mode Exit fullscreen mode

A bytes can be unpacked with an assignment and for statement, function and * but not with ** as shown below:

v1, v2, v3 = b'ABC'

print(v1, v2, v3)
# 65 66 67
Enter fullscreen mode Exit fullscreen mode
v1, *v2, v3 = b'ABCDEF'

print(v1, v2, v3)  # 65 [66, 67, 68, 69] 70
print(v1, *v2, v3) # 65 66 67 68 69 70
Enter fullscreen mode Exit fullscreen mode
for v1, v2, v3 in [b'ABC', b'DEF']:
    print(v1, v2, v3)
# 65 66 67
# 68 69 70
Enter fullscreen mode Exit fullscreen mode
for v1, *v2, v3 in [b'ABCDEF', b'GHIJKL']:
    print(v1, v2, v3)
    print(v1, *v2, v3)
# 65 [66, 67, 68, 69] 70
# 65 66 67 68 69 70
# 71 [72, 73, 74, 75] 76
# 71 72 73 74 75 76
Enter fullscreen mode Exit fullscreen mode
print(*b'ABCD', *b'EF')
# 65 66 67 68 69 70
Enter fullscreen mode Exit fullscreen mode
print([*b'ABCD', *b'EF'])
# [65, 66, 67, 68, 69, 70]
Enter fullscreen mode Exit fullscreen mode
def func(p1='a', p2='b', p3='c', p4='d', p5='e', p6='f'):
    print(p1, p2, p3, p4, p5, p6)

func()
# a b c d e f

func(*b'ABCD', *b'EF')
# 65 66 67 68 69 70
Enter fullscreen mode Exit fullscreen mode
def func(p1='a', p2='b', *args):
    print(p1, p2, args)
    print(p1, p2, *args)
    print(p1, p2, [0, 1, *args, 2, 3])

func()
# a b ()
# a b Nothing
# a b [0, 1, 2, 3]

func(*b'ABCD', *b'EF')
# 65 66 (67, 68, 69, 70)
# 65 66 67 68 69 70
# 65 66 [0, 1, 67, 68, 69, 70, 2, 3]
Enter fullscreen mode Exit fullscreen mode

Be careful, a big bytes gets OverflowError as shown below:

v = b'ABCDE' * 1000000000

print(v)
# OverflowError: repeated bytes are too long
Enter fullscreen mode Exit fullscreen mode

Top comments (0)