I don't understand Python bytes objects, its purpose and when to use it?
Can someone please explain what is bytes in Python and why we need it?
What is Python's bytes type actually used for? - Stack Overflow
python - Why does "bytes(n)" create a length n byte string instead of converting n to a binary representation? - Stack Overflow
I am haveing some hard time here trying to understand Python's bytes object. Why it was intruduced, when to use it? and Why to use it? Why older python didn't have bytes object, and things were kind ok, and they came up with the ideas to invent bytes object?
Appraciate if someone could point to an artice/video where bytes object is explaned like if I am five years old.
Thanks.
Well I am having little bit hard time trying to understand what are bytes (object, data model) and why we need it. I know there are lists, dictionaries, string and all that data types. But what is bytes?
Possible duplicate of what is the difference between a string and a byte string
In short, the bytes type is a sequence of bytes that have been encoded and are ready to be stored in memory/disk. There are many types of encodings (utf-8, utf-16, windows-1255), which all handle the bytes differently. The bytes object can be decoded into a str type.
The str type is a sequence of unicode characters. The str needs to be encoded to be stored, but is mutable and an abstraction of the bytes logic.
There is a strong relationship between str and bytes. bytes can be decoded into a str, and strs can be encoded into bytes.
You typically only have to use bytes when you encounter a string in the wild with a unique encoding, or when a library requires it. str , especially in python3, will handle the rest.
More reading here and here
Sockets is a good case for using bytes. Also you should use bytes to read/write binary data, like image or audio, from a file or a web API. The str type is an immutable sequence of character, which is typically UTF8 encoded. Obviously if your data is not characters, then doing the UTF8 encoding on it will be inefficient and could cause bugs.
From python 3.2 you can use to_bytes:
>>> (1024).to_bytes(2, byteorder='big')
b'\x04\x00'
def int_to_bytes(x: int) -> bytes:
return x.to_bytes((x.bit_length() + 7) // 8, 'big')
def int_from_bytes(xbytes: bytes) -> int:
return int.from_bytes(xbytes, 'big')
Accordingly, x == int_from_bytes(int_to_bytes(x)).
Note that the above encoding works only for unsigned (non-negative) integers.
For signed integers, the bit length is a bit more tricky to calculate:
def int_to_bytes(number: int) -> bytes:
return number.to_bytes(length=(8 + (number + (number < 0)).bit_length()) // 8, byteorder='big', signed=True)
def int_from_bytes(binary_data: bytes) -> Optional[int]:
return int.from_bytes(binary_data, byteorder='big', signed=True)
That's the way it was designed - and it makes sense because usually, you would call bytes on an iterable instead of a single integer:
>>> bytes([3])
b'\x03'
The docs state this, as well as the docstring for bytes:
>>> help(bytes)
...
bytes(int) -> bytes object of size given by the parameter initialized with null bytes