[Tutor] Re: Re: Why is this write defined as a tuple, instead of picking list?

Andrei project5 at redrival.net
Sat Apr 17 16:58:00 EDT 2004


Andy Baker wrote on Sat, 17 Apr 2004 15:12:15 +0100:

>>> It is generally not recommended to use this, because it's slow. It's
>> better
>>> to have a list of substrings like this:
>>>
>>>   write = ["a", "b", "c"]
>>>
>>> and then use the string method join() to convert that list into a
>> string

> Yuk!

I'll have to disagree with this "Yuk". This method is Python idiom and is
extremely well known an extremely frequently used (unlike string
concatenation). The join() method may seem a little awkward at first
(particularly the fact that it's a string method instead of a list method
like one would most likely expect), but it's much better and more clear
than string concatenation. Let's assume you want to write a set of values
to a comma-separated list - just the kind of task join() is meant for. You
advocate using something like this:

  s = ""
  for elem in mylist:
      s = s + ", " + elem # s will start with ", "
  s = s.strip(", ") # need to remove leading ", "

You'll probably agree that it's clearer to write:

  s = ", ".join(mylist)

>> Or even use a format operator with the tuple:
>> 
>>     write = "%s%s%s" % ("a", "b", "c')
> 
> Double Yuk!

I disagree even more with you disliking this - remember, this example is
contrived and only an idiot would do this instead of 'write = "abc"', it's
just meant to demonstrate the use of the formatting operator. 

String concatentation is a pain in the butt compared to string formatting.
It's hard to maintain, slow and will get you in trouble if you ever plan on
e.g. internationalizing your application (unless you enjoy going over
existing code to change all string concatentations to format strings). I
wouldn't recommend string concatenation instead of string formatting even
if string concatenation was fast.

And again, formatting improves readability, it does not lessen it, just
like writing a simple "5" improves readability compared to writing "3 + 2".
An example:

  farm = pig = horse = 1
  print "Old MacDonald had: %d farm, %d pig, %d horse" % (farm, pig, horse)

versus:

  farm = pig = horse = 1
  print "Old MacDonald had: " + str(farm) + " farm, " + str(pig) + " pig, "
+ str(horse) + " horse"

Where string formatting is used, it's immediately clear what the output
will look like: something mentioning the amount of farms, pigs and horses.
In the second case you'll have to parse the string concatenation mentally
in order to find out what will be printed.

>>>   write = "<h3>%s</h3>" % article_items[0]

> I would prefer to go with concantenation for readabilityin most cases. Why
> is concantenation so slow and could the Python developers speed it up rather
> than force us to use the uglier syntax?

String concatenation is not readable, as I demonstrated above. If you're
just concatenating "a" and "b", you can use concatenation, it's not such an
important speed penalty and readability doesn't suffer. But when large
amounts (i.e. more than two pieces) of data need to be concatenated to a
single string, the other two methods of string building are both faster and
more readable.

-- 
Yours,

Andrei

=====
Real contact info (decode with rot13):
cebwrpg5 at jnanqbb.ay. Fcnz-serr! Cyrnfr qb abg hfr va choyvp cbfgf. V ernq
gur yvfg, fb gurer'f ab arrq gb PP.




More information about the Tutor mailing list