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

Chris Angelico rosuav at gmail.com
Tue Mar 8 19:36:09 EST 2016


On Wed, Mar 9, 2016 at 11:09 AM, BartC <bc at freeuk.com> wrote:
> On 08/03/2016 23:28, Steven D'Aprano wrote:
>>
>> 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.

Please, please, PLEASE get an understanding of the difference between
text and bytes. The fact that the byte string in Py2 happens to have
the same name as the text string in Py3 should be considered a
*coincidence* when you're discussing these differences.

Whenever you open a file in binary mode, you get back bytes. In Python
2, opening a file in text mode still gives back bytes, which is
anomalous. Python 3 handles this properly: when you open a file in
text mode, you specify an encoding, and it is decoded to Unicode text.

>> 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 that bothers you, open the file in binary mode. Then you get a
stream of bytes. In text mode, you're declaring that you expect the
file to be a stream of lines. Lines might be ended by \r\n or \n, and
you usually don't care. (Sometimes you do. Often you don't.)

> 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...

It's no difference from the question of when you use a tuple and when
you use a list. They're very similar object types, with nearly the
same methods and operations available. One's mutable and one's not.
When should you use a tuple? Is it just a frozenlist? These are the
sorts of questions that keep coming up on this list, and that's not a
problem. They're coding design questions. When should you use an
float, when a fractions.Fraction, when decimal.Decimal, etc? The fact
that people have to ask does not mean the language should lose some of
them.

If someone asked me when I should use Windows Services and when I
should use the Scheduler, I wouldn't know. If ever the situation comes
up, I'd probably have to ask. That's not a problem; that's what
mailing lists are for.

ChrisA



More information about the Python-list mailing list