Pyhon 2.x or 3.x, which is faster?

BartC bc at freeuk.com
Tue Mar 8 19:09:56 EST 2016


On 08/03/2016 23:28, Steven D'Aprano wrote:
> On Tue, 8 Mar 2016 10:53 pm, BartC wrote:

>>                 Python 2     Python 3
>>
>> Text mode     'str'          'str'
>> Binary mode   'bytes'        'str'
>
> That table is incorrect. In Python 2, bytes is just an alias for str:
>
> [steve at ando ~]$ python2.7 -c "print bytes is str"
> True
>
>
> and reading from a file returns a result depending on whether it is opened
> in text or binary mode:
>
> [steve at ando ~]$ echo foo > /tmp/junk
> [steve at ando ~]$ python2.7 -c "print type(open('/tmp/junk', 'r').read())"
> <type 'str'>
> [steve at ando ~]$ python2.7 -c "print type(open('/tmp/junk', 'rb').read())"
> <type 'str'>
>
>
> In Python 3, you get bytes in binary mode, and Unicode strings (known
> as "str") in text mode:
>
> [steve at ando ~]$ python3 -c "print(type(open('/tmp/junk', 'r').read()))"
> <class 'str'>
> [steve at ando ~]$ python3 -c "print(type(open('/tmp/junk', 'rb').read()))"
> <class 'bytes'>
>
>
> Here it is in a table form:
>
> --------  --------  --------
> Mode      Python 2  Python 3
> --------  --------  --------
> Text      bytes     text
> Binary    bytes     bytes
> --------  --------  --------

That doesn't correspond to your results above. Going with the reported 
type of what is read, it would be:

  --------  --------  --------
  Mode      Python 2  Python 3
  --------  --------  --------
  Text      str       str
  Binary    str       bytes
  --------  --------  --------

Notice the odd-one-out is now the bottom left, not the the top right.

> Python will never expand \n to \r\n. But it may translate \r\n to \n.

Well, OK, you might lose bytes with the value 13. That's also bad.

>> If you pass a file-handle to a function, that function can't just do a
>> read from that handle without considering whether the file might be in
>> text or binary mode, or whether it's running under Py2 or Py3.
>
> Why not? file.read() works exactly the same in all four cases.

Didn't you just demonstrate that it can give different results?


> It's hard to argue against your statement when I don't understand what
> problem you think you have. A concrete example of this "mess" would help.
>
>> And I think someone pointed out the difference between 'bytes',
>> 'bytearray' and 'array.array', but I can't find that post at the minute.
>
> Er, yes? Of course there is a differences between those three types. There's
> also a difference between list, set and dict. What's your point?

OK, I take it back; if it's not a mess, then it's just confusing!

If someone asked me when I'd use a byte sequence, and when I'd use a 
bytearray object, then I wouldn't have a clue.

(Looking it up, it appears than one is mutable, and the other isn't. 
This isn't quite the same as the difference between a list and a dict.)

As for array.arrays, those apparently can also be arrays of bytes. 
Presumably, mutable. OK. Definitely nothing here that could be tidied up...

-- 
Bartc






More information about the Python-list mailing list