[Python-ideas] duck typing for io write methods

Paul Moore p.f.moore at gmail.com
Sat Jun 15 17:52:05 CEST 2013


On 15 June 2013 15:45, Wolfgang Maier <
wolfgang.maier at biologie.uni-freiburg.de> wrote:

> Two questions though: you're saying in 3.3+. Does that mean the behaviour
> has changed with 3.3 or that you checked it only for that version (I'm
> currently using 3.2)?
> Second, is that one byte optimization special for str() from int or is it
> happening elsewhere too (like in string literals without non-english
> characters)? Where can I find that documented?
>

Basically, it's new in Python 3.3. See the What's New document at
http://docs.python.org/3/whatsnew/3.3.html#pep-393 and PEP 393 (
http://www.python.org/dev/peps/pep-0393/)

What happened is that the internal representation of strings changed so
that strings are held in 1, 2 or 4-byte form depending on the actual data.
So all-ASCII data (such as the numbers you are interested in) are held in
1-byte form, and encoding to and from bytes can be done by just copying the
bytes (assuming you're using an ascii-compatible encoding).

The same code works in earlier versions, but it will be slower (how much
depends on your application) because bytestrings will need to be converted
to and from wide character strings.

Paul.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130615/e299796b/attachment.html>


More information about the Python-ideas mailing list