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

Steven D'Aprano steve at pearwood.info
Tue Mar 8 21:01:53 EST 2016


On Wed, 9 Mar 2016 11:34 am, Michael Torrie wrote:

> There are some interesting differences I found between a Python 2 string
> (composed of bytes) and a Python 3 byte string, such as what you'd get
> from calling read() on a file handle opened in binary mode.  That is in
> Python 2, indexing a string returns a string of length 1.  In Python
> 3.5, indexing a byte string returns a value, the equivalent of calling
> ord() on the single byte string.  

Yes, this sadly turned out to be a mistake, and one which we're probably
stuck with.

> This makes it a bit difficult to make 
> the code easily work between Python 2 and 3 and handle bytes.  Any ideas
> there?


Use a single byte slice:

the_bytes[i:i+1]


which works identically in Python 2 and 3.



-- 
Steven




More information about the Python-list mailing list