In Python 2.6, bytes is str
Benjamin Kaplan
benjamin.kaplan at case.edu
Mon Oct 6 08:05:53 EDT 2008
On Mon, Oct 6, 2008 at 1:30 AM, Bryan Olson <fakeaddress at nowhere.org> wrote:
>
>
> In Python 2.6, the name 'bytes' is defined, and bound to str. The 2.6
> assignment presents some pitfalls. Be aware.
>
> Consider constructing a bytes object as:
>
> b = bytes([68, 255, 0])
>
> In Python 3.x, len(b) will be 3, which feels right.
>
> In Python 2.6, len(b) will be 12, because b is the str, '[68, 255, 0]'.
>
>
> I'm thinking I should just avoid using 'bytes' in Python 2.6. If there's
> another Python release between 2.6 and 3.gold, I'd advocate removing the
> pre-defined 'bytes', or maybe defining it as something else.
You could just use a bytearray instead of a normal list.
Python 2.6 (trunk:66714:66715M, Oct 1 2008, 18:36:04)
[GCC 4.0.1 (Apple Computer, Inc. build 5370)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> a = bytearray([65,66,67])
>>> a
bytearray(b'ABC')
>>> a[1] = 70
>>> a
bytearray(b'AFC')
>>> bytes(a)
'AFC'
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20081006/a6494f77/attachment-0001.html>
More information about the Python-list
mailing list