How do I 'declare' an empty bytes variable?

Question:

How do I initialize (‘declare’) an empty bytes variable in Python 3?

I am trying to receive chunks of bytes, and later change that to a
utf-8 string.
However, I’m not sure how to initialize the initial variable that will
hold the entire series of bytes. This variable is called msg.
I can’t initialize it as None, because you can’t add a bytes and a
NoneType. I can’t initialize it as a unicode string, because then
I will be trying to add bytes to a string.
Also, as the receiving program evolves it might get me in to a mess
with series of bytes that contain only parts of characters.
I can’t do without a msg initialization, because then msg would be
referenced before assignment.
The following is the code in question

def handleClient(conn, addr):
    print('Connection from:', addr)
    msg = ?
    while 1:
        chunk = conn.recv(1024)
        if not chunk:
            break
        msg = msg + chunk
    msg = str(msg, 'UTF-8')
    conn.close()
    print('Received:', unpack(msg))
Asked By: tsteemers

||

Answers:

Just use an empty byte string, b''.

However, concatenating to a string repeatedly involves copying the string many times. A bytearray, which is mutable, will likely be faster:

msg = bytearray()  # New empty byte array
# Append data to the array
msg.extend(b"blah")
msg.extend(b"foo") 

To decode the byte array to a string, use msg.decode(encoding='utf-8').

Answered By: Mechanical snail

As per documentation:

Blockquote
socket.recv(bufsize[, flags])
Receive data from the socket. The return value is a string representing the data received.
Blockquote
So, I think msg=”” should work just fine:

>>> msg = ""
>>> msg
''
>>> len(msg)
0
>>>
Answered By: PSS

Use msg = bytes('', encoding = 'your encoding here').

Encase you want to go with the default encoding, simply use msg = b'', but this will garbage the whole buffer if its not in the same encoding

Answered By: Dr. Sahib

bytes() works for me;

>>> bytes() == b''
True
Answered By: jorijnsmit

To allocate bytes of some arbitrary length do

bytes(bytearray(100))
Answered By: philn