"Byte" type?

Benjamin Kaplan benjamin.kaplan at case.edu
Sun Feb 15 18:16:10 CET 2009


On Sun, Feb 15, 2009 at 11:57 AM, John Nagle <nagle at animats.com> wrote:

> Benjamin Peterson wrote:
>
>> Steve Holden <steve <at> holdenweb.com> writes:
>>
>>> Beware, also, that in 2.6 the "bytes" type is essentially an ugly hack
>>> to enable easier forward compatibility with the 3.X series ...
>>>
>>
>> It's not an ugly hack. It just isn't all that you might hope it'd live up
>> to be.
>>
>
>   The semantics aren't what the naive user would expect.  One would
> expect an element of a bytearray to be a small integer.  But instead,
> it has string-like behavior.  "+" means concatenate, not add.
> The bit operators don't work at all.
>

Because b'x' is NOT a bytearray. It is a bytes object. When you actually use
a bytearray, it behaves like you expect.
>>> type(b'x')
<class 'bytes'>
>>> type(bytearray(b'x'))
<class 'bytearray'>
>>> ba = bytearray(b'abc')
>>> ba[0] + ba[1]
195



>
> Python 2.6.1 ...
> >>> a = b'A'
> >>> b = b'B'
> >>> a+b
> 'AB'
> >>> a[0]+b[0]
> 'AB'
> >>>>>> a = b'A'
> >>> b = b'B'
> >>> a+b
> 'AB'
> >>> a[0]+b[0]
> 'AB'
> >>>
> >>> a & b
> Traceback (most recent call last):
>  File "<stdin>", line 1, in <module>
> TypeError: unsupported operand type(s) for &: 'str' and 'str'
>
>   Given that the intent of bytearray is that it's a data type for
> handling raw binary data of unknown format, one might expect it to behave
> like
> "array.array('B')", which is an array of unsigned bytes that are
> treated as integers.  But that's not how "bytearray" works.  "bytearray"
> is more like the old meaning of "str", before Unicode support, circa Python
> 2.1.
>
>   I sort of understand the mindset, but the documentation needs to be
> improved.
> Right now, we have a few PEPs and the 2.6 "New features" article, but
> no comprehensive documentation.  The relationship between "str", "unicode",
> "bytearray", "array.array('B')", and integers, and how this changes from
> version to version of Python, needs to be made clearer, or conversion
> to 2.6/3.0 will not happen rapidly.
>
>                                John Nagle
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090215/e34995d2/attachment.html>


More information about the Python-list mailing list