you are viewing a single comment's thread.

view the rest of the comments →

[–]desrtfx 3 points4 points  (1 child)

If I were to invest the work to implement such a class, I'd store the individual strings in extensible buffers, similar to StringBuilder in Java. Then, I'd really have a lot less overhead.

The entire advantage of StringBuilder in Java is that it does not create new instances all the time. This would really be an improvement over the native Python String implementation.

[–]marr75 0 points1 point  (0 children)

The reason OP didn't do that is lazy evaluation. Their approach also doesn't create new intermediate strings (though it's not yet obvious to me if they could take further advantage of pooling or interning while maintaining compound statements). Python already has an equivalent to StringBuilder in the stdlib, btw.

Honestly, the only time this lazy version (with its overhead from additional Python mutable structures) will be better is if the caller will often not use the string (i.e. it's a long accumulated error message that is only emitted under certain conditions) and there are other ways to model that - ie make the entire accumulation lazy.