これは、コードとコメントで最もよく説明されていると思います。
import struct
class binary_buffer(str):
def __init__(self, msg=""):
self = msg
def write_ubyte(self, ubyte):
self += struct.pack("=B", ubyte)
return len(self)
Output
>> bb = binary_buffer()
>> bb # Buffer starts out empty, as it should
''
>> bb.write_ubyte(200)
1 # We can see that we've successfully written one byte to the buffer
>> bb
'' # Huh? We just wrote something, but where has it gone?