Python inheritance and extension bytearray
I want to be able to write to the bytearray buffer and clear it by calling the method, so I have a class that looks like this:
import struct
class binary_buffer(bytearray):
def __init__(self, message=""):
self = message
def write_ubyte(self, ubyte):
self += struct.pack("=B", ubyte)
return len(self)
def clear(self):
self = ""
However, calling clear () doesn't seem to do anything at all. Sample output will look like this:
>>> bb = binary_buffer('')
>>> bb
bytearray(b'') # As expected, the bytearray is empty
>>> bb.write_ubyte(255)
1 # Great, we just wrote a unsigned byte!
>>> bb
bytearray(b'\xff') # Looking good. We have our unsigned byte in the bytearray.
>>> bb.clear() # Lets start a new life!
>>> bb
bytearray(b'\xff') # Um... I though I just cleared out the trash?
+3
source to share