Receive data from socket stream

Hrvoje Niksic hniksic at
Mon Apr 28 16:28:26 CEST 2008

Nick Craig-Wood <nick at> writes:

>>  Note that appending to a string is almost never a good idea, since it
>>  can result in quadratic allocation.
> My aim was clear exposition rather than the ultimate performance!

That would normally be fine.  My post wasn't supposed to pick
performance nits, but to point out potentially quadratic behavior.

> Anyway str += was optimised in python 2.4 or 2.5 (forget which) wasn't
> it?

That optimization works only in certain cases, when working with
uninterned strings with a reference count of 1, and then only when the
strings are in stored local variables, rather than in global vars or
in slots.  And then, it only works in CPython, not in other
implementations.  The optimization works by "cheating" -- breaking the
immutable string abstraction in the specific cases in which it is
provably safe to do so.
examines it in some detail.

Guido was reluctant to accept the patch that implements the
optimization because he thought it would "change the way people write
code", a sentiment expressed in
This discussion shows that he was quite right in retrospect.  (I'm not
saying that the optimization is a bad thing, just that it is changing
the "recommended" way of writing Python in a way that other
implementations cannot follow.)

More information about the Python-list mailing list