*Memo:
- My post explains a bytes (1).
- My post explains a bytes (3).
- My post explains a bytes (4).
- My post explains bytes().
A bytes can be enlarged with * and a number as shown below:
v = b'ABCDE' * 3
print(v)
# b'ABCDEABCDEABCDE'
v = b'01234' * 3
print(v)
# b'012340123401234'
v = b'' * 3
print(v)
# b''
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'
A bytes object and other bytes objects 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).
print(b'AE' | b'ACE' | b'ABDE')
# TypeError: unsupported operand type(s) for |: 'bytes' and 'bytes'
print(b'AE' & b'ACE' & b'ABDE')
# TypeError: unsupported operand type(s) for &: 'bytes' and 'bytes'
print(b'AE' - b'ACE' - b'ABDE')
# TypeError: unsupported operand type(s) for -: 'bytes' and 'bytes'
A bytes object and other bytes object cannot return the bytes in the bytes object but not in other bytes object or not in the bytes object but in other bytes object with '^' (Symmetric Difference: A Δ B) as shown below:
print(b'ABCD' ^ b'ACE')
# TypeError: unsupported operand type(s) for ^: 'bytes' and 'bytes'
A bytes can be iterated with a for statement as shown below:
for v in b'ABC':
print(v)
# 65
# 66
# 67
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
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
for v1, v2, v3 in [b'ABC', b'DEF']:
print(v1, v2, v3)
# 65 66 67
# 68 69 70
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
print(*b'ABCD', *b'EF')
# 65 66 67 68 69 70
print([*b'ABCD', *b'EF'])
# [65, 66, 67, 68, 69, 70]
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
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]
Be careful, a big bytes gets OverflowError as shown below:
v = b'ABCDE' * 1000000000
print(v)
# OverflowError: repeated bytes are too long
Top comments (0)