"John Nagle" <[email protected]> wrote in message
news:[email protected]...
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.
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.
It *is* the old meaning of str. It isn't a bytearray object in 2.6.X (and
it isn't a bytearray object in 3.X either, but a bytes object):
Python 2.6.1 (r261:67517, Dec 4 2008, 16:51:00) [MSC v.1500 32 bit (Intel)]
on win32
Type "help", "copyright", "credits" or "license" for more information.
type(b'x')
<type 'str'>
b'x'[0]
'x'
As Steve said, it is just an aliased type. In 3.X it is really a bytes
object:
Python 3.0.1 (r301:69561, Feb 13 2009, 20:04:18) [MSC v.1500 32 bit (Intel)]
on win32
Type "help", "copyright", "credits" or "license" for more information.
type(b'x')
<class 'bytes'>
b'x'[0]
120
-Mark
--
http://mail.python.org/mailman/listinfo/python-list