Here is a method tested in both python2.x and python3.x:
from array import array
class MyBinaryBuffer(array):
def __new__(cls, *args, **kwargs):
return super(MyBinaryBuffer, cls).__new__(cls, 'B', *args, **kwargs)
b1 = MyBinaryBuffer([1, 2, 3])
b2 = MyBinaryBuffer()
print(b1, b2)
or if you want to avoid using super:
class MyBinaryBuffer(array):
def __new__(cls, *args, **kwargs):
return array.__new__(cls, 'B', *args, **kwargs)
In this particular case, you can fully emulate the behavior array.array, other existing answers do not guarantee this.
source
share