, python , bytes interface bytearray. @MarkPerryman , .encode() .decode() :
from array import array
class StringArray(array):
def __new__(cls,code,start=''):
if code != "b":
raise TypeError("StringArray must use 'b' typecode")
if isinstance(start,str):
start = start.encode()
return array.__new__(cls,code, start)
def fromstring(self,s):
return self.frombytes(s.encode())
def tostring(self):
return self.tobytes().decode()
x = StringArray('b','test')
print(x.tostring())
x.fromstring("again")
print(x.tostring())