Rance Hall wrote:
Ok so I know what I am doing is deprecated (or at least poor form) but
the replacement must be awkward cause I'm not getting it.
[...]
message = "Bah."
if test:
   message = message + " Humbug!"


It's not deprecated, nor is it poor form. However, it can be abused, or perhaps *misused* is a better term, and it is the misuse you need to watch out for.

Somebody posted a similar comment on another Python list today, which I answered, so I'll copy and paste my answer here:


There's nothing wrong with concatenating (say) two or three strings.
What's a bad idea is something like:

s = ''
while condition:
    s += "append stuff to end"

Even worse:

s = ''
while condition:
    s = "insert stuff at beginning" + s

because that defeats the runtime optimization (CPython only!) that
*sometimes* can alleviate the badness of repeated string concatenation.

See Joel on Software for more:

http://www.joelonsoftware.com/articles/fog0000000319.html

But a single concatenation is more or less equally efficient as string
formatting operations (and probably more efficient, because you don't
have the overheard of parsing the format mini-language).

For repeated concatenation, the usual idiom is to collect all the
substrings in a list, then join them all at once at the end:

pieces = []
while condition:
    pieces.append('append stuff at end')
s = ''.join(pieces)




--
Steven

_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to