I am getting TypeError: 'bytearray' object doesn't support item deletion
Any idea for workaround?
-aha
Code: Select all
buffer = bytearray()
...
del buffer[0:i + 1]
Code: Select all
buffer = bytearray()
...
del buffer[0:i + 1]
Even if "del <bytearray>" was supported, I'm not sure there's a way to avoid a copy. (Unless I'm missing something -- what did you have in mind?)
Code: Select all
buffer[0:i + 1] = b''
If your goal is to minimise memory usage, then this might possibly be the more efficient option. As with the previous example, it temporarily results in two copies of the buffer, but the new copy will be sized exactly correctly, and the GC will clean up the old one, so the resulting state is overall that there are `i` total fewer bytes allocated.
Code: Select all
lba = 1000 # size of bytearray
n = 10 # number of bytes to delete
ba = bytearray(lba)
mba = memoryview(ba)
mba[0:-n] = mba[n:]
I don't think it does.
Code: Select all
n = 10 # number of bytes to delete
ba = bytearray(range(20))
print(ba)
mba = memoryview(ba)
mba[0:-n] = mba[n:]
print(bytes(mba))
print(ba)
Code: Select all
bytearray(b'\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f\x10\x11\x12\x13')
b'\n\x0b\x0c\r\x0e\x0f\x10\x11\x12\x13\n\x0b\x0c\r\x0e\x0f\x10\x11\x12\x13'
bytearray(b'\n\x0b\x0c\r\x0e\x0f\x10\x11\x12\x13\n\x0b\x0c\r\x0e\x0f\x10\x11\x12\x13')