<br><br><div class="gmail_quote">On Sun, Feb 15, 2009 at 11:57 AM, John Nagle <span dir="ltr"><<a href="mailto:nagle@animats.com">nagle@animats.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Benjamin Peterson wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><div class="Ih2E3d">
Steve Holden <steve <at> <a href="http://holdenweb.com" target="_blank">holdenweb.com</a>> writes:<br>
</div><div class="Ih2E3d"><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Beware, also, that in 2.6 the "bytes" type is essentially an ugly hack<br>
to enable easier forward compatibility with the 3.X series ...<br>
</blockquote>
<br></div><div class="Ih2E3d">
It's not an ugly hack. It just isn't all that you might hope it'd live up to be.<br>
</div></blockquote>
<br>
   The semantics aren't what the naive user would expect.  One would<br>
expect an element of a bytearray to be a small integer.  But instead,<br>
it has string-like behavior.  "+" means concatenate, not add.<br>
The bit operators don't work at all.<br>
</blockquote><div><br>Because b'x' is NOT a bytearray. It is a bytes object. When you actually use a bytearray, it behaves like you expect.<br>>>> type(b'x')<br><class 'bytes'><br>>>> type(bytearray(b'x'))<br>
<class 'bytearray'><br>>>> ba = bytearray(b'abc')<br>>>> ba[0] + ba[1]<br>195<br><br> <br></div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<br>
Python 2.6.1 ...<br>
>>> a = b'A'<br>
>>> b = b'B'<br>
>>> a+b<br>
'AB'<br>
>>> a[0]+b[0]<br>
'AB'<br>
>>>>>> a = b'A'<br>
>>> b = b'B'<br>
>>> a+b<br>
'AB'<br>
>>> a[0]+b[0]<br>
'AB'<br>
>>><br>
>>> a & b<br>
Traceback (most recent call last):<br>
  File "<stdin>", line 1, in <module><br>
TypeError: unsupported operand type(s) for &: 'str' and 'str'<br>
<br>
   Given that the intent of bytearray is that it's a data type for<br>
handling raw binary data of unknown format, one might expect it to behave like<br>
"array.array('B')", which is an array of unsigned bytes that are<br>
treated as integers.  But that's not how "bytearray" works.  "bytearray"<br>
is more like the old meaning of "str", before Unicode support, circa Python 2.1.<br>
<br>
   I sort of understand the mindset, but the documentation needs to be improved.<br>
Right now, we have a few PEPs and the 2.6 "New features" article, but<br>
no comprehensive documentation.  The relationship between "str", "unicode",<br>
"bytearray", "array.array('B')", and integers, and how this changes from<br>
version to version of Python, needs to be made clearer, or conversion<br>
to 2.6/3.0 will not happen rapidly.<br><font color="#888888">
<br>
                                John Nagle</font><div><div></div><div class="Wj3C7c"><br>
--<br>
<a href="http://mail.python.org/mailman/listinfo/python-list" target="_blank">http://mail.python.org/mailman/listinfo/python-list</a><br>
</div></div></blockquote></div><br>