[Python-Dev] Re: adding a bytes sequence type to Python

Skip Montanaro skip at pobox.com
Sat Aug 28 22:58:49 CEST 2004


    >> > Is there any consensus forming on whether bytes() instances are
    >> > mutable or not?
    >> 
    >> Mutable!

    Phillip> So, how will it be different from:

    Phillip>      from array import array

    Phillip>      def bytes(*initializer):
    Phillip>          return array('B',*initializer)

That might well be a decent trial implementation, though it seems that if we
allow strings as initializers we should also allow strings in assignment:

    >>> b = bytes("abc\xff")
    >>> b
    array('B', [97, 98, 99, 255])
    >>> print buffer(b)
    abcÿ
    >>> b[3] = '\xfe'
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
    TypeError: an integer is required
    >>> b += "\xfe"
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
    TypeError: can only extend array with array (not "str")

I must admit I'm having a bit of trouble getting past this point with a more
traditional subclass (can array objects not be subclassed?) in part I think
because I don't understand new-style classes very well.  In particular, I
couldn't find a description of __new__, and once I fumbled past that, I
didn't seem to be able to override append() or extend().

Skip


More information about the Python-Dev mailing list