Minor, minor style question

Christian Tanzer tanzer at swing.co.at
Sat Mar 2 02:34:47 EST 2002


claird at starbase.neosoft.com (Cameron Laird) wrote:

> Why would I prefer
>   string = ""
>   string = string + "abc "
>   string = string + "def "
>   string = string + "xyz"
> over
>   string = "abc " + \
> 	      "def " + \
> 	      "xyz"
> ?

Why not use:

  string = ("abc"
            "def"
            "xyz"
           )

for constant strings, and

  string = "".join((abc, def, xyz))

for variable strings? Both are more robust, way more efficient, and
(to me, at least) easier to read.

BTW, beware of:

  result = ""
  for s in ... :
    result = result + mumble(s)

For many elements, this gets really slow. Appending to a list and joining
at the end is much faster.

-- 
Christian Tanzer                                         tanzer at swing.co.at
Glasauergasse 32                                       Tel: +43 1 876 62 36
A-1130 Vienna, Austria                                 Fax: +43 1 877 66 92





More information about the Python-list mailing list