views:

1029

answers:

3

I'm experimenting with bytes vs bytearray in Python 2.6. I don't understand the reason for some differences.

A bytes iterator returns strings:

for i in bytes(b"hi"):
    print(type(i))

Gives:

<type 'str'>
<type 'str'>

But a bytearray iterator returns ints:

for i in bytearray(b"hi"):
    print(type(i))

Gives:

<type 'int'>
<type 'int'>

Why the difference?

I'd like to write code that will translate well into Python 3. So, is the situation the same in Python 3?

+2  A: 

I am not sure since which version, but bytes is actually a str, which you can see if you do type(bytes(b"hi")) -> <type 'str'>.

bytearray is a mutable array of bytes, one constructor of which takes a string.

van
+5  A: 

In Python 2.6 bytes is merely an alias for str.
This "pseudo type" was introduced to [partially] prepare programs [and programmers!] to be converted/compatible with Python 3.0 where there is a strict distinction of semantics and use for str (which are systematically unicode) and bytes (which are arrays of octets, for storing data, but not text)

Similarly the b prefix for string literals is ineffective in 2.6, but it is a useful marker in the program, which flags explicitly the intent of the programmer to have the string as a data string rather than a text string. This info can then be used by the 2to3 converter or similar utilities when the program is ported to Py3k.

You may want to check this SO Question for additional info.

mjv
For more info see: http://docs.python.org/whatsnew/2.6.html#pep-3112-byte-literals and http://docs.python.org/3.1/library/stdtypes.html#sequence-types-str-bytes-bytearray-list-tuple-range
Ned Deily
A: 

I tried it on Python 3.0.

In Python 3.0, a bytes iterator returns ints, not strings as Python 2.6 did:

for i in bytes(b"hi"):
    print(type(i))

Gives:

<class 'int'>
<class 'int'>

A bytearray iterator also returns class 'int's.

Craig McQueen