Python Interview Questions

Rhamphoryncus rhamph at gmail.com
Wed Oct 31 18:31:44 EDT 2007


On Oct 31, 2:58 am, konryd <kon... at gmail.com> wrote:
> > - string building...do they use "+=" or do they build a list
> >    and use .join() to recombine them efficiently
>
> I'm not dead sure about that, but I heard recently that python's been
> optimized for that behaviour. That means: using += is almost as fast
> as joining list. Besides, "+=" is more obvious than the other choice
> and since performance is not a problem until it's a problem, I'd
> rather use it. Anyway: I wouldn't pick it for an interview question.

Concatenating strings using += will often perform quadratically with
the number of concatenations.  Your testing will likely use a small
number and not take a noticeable amount of time.  Then it'll get
deployed and someone will plug in a much larger number, wondering why
the program is so slow.  The recent optimizations just make it more
obscure.

+= shouldn't be an obvious choice for sequences.  If it's mutable,
use .append().  If it's immutable, build up in a mutable sequence,
then convert.

--
Adam Olsen, aka Rhamphoryncus




More information about the Python-list mailing list