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

BartC bc at freeuk.com
Tue Mar 8 06:53:40 EST 2016


On 08/03/2016 02:12, Steven D'Aprano wrote:
> On Tue, 8 Mar 2016 09:39 am, BartC wrote:

>> I'm using it because this kind of file reading in Python is a mess. If I
>> do a read, will I get a string, a byte sequence object, a byte-array, or
>> array-array, or what?
>
> Calling it "a mess" is an exaggeration. There is a change between Python 2
> and 3:
>
> - in Python 2, reading from a file gives you bytes, that is, the
> so-called "str" type, not unicode;
>
> - in Python 3, reading from a file in binary mode gives you bytes, that is,
> the "bytes" type; reading in text mode gives you a string, the "str" type.
>
> How is this a mess?

               Python 2     Python 3

Text mode     'str'          'str'
Binary mode   'bytes'        'str'

For certain file formats, text mode can't be used because byte values 
such as 10 could be expanded to the two bytes 13,10 on some systems.

So binary needs to be used. But Py2 and Py3 return different results; 
and indexing a bytes object gives you an int, a str object gives you a str.

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.

And I think someone pointed out the difference between 'bytes', 
'bytearray' and 'array.array', but I can't find that post at the minute.

-- 
bartc





More information about the Python-list mailing list