StringIO proposal: add __iadd__

Paul Rubin http
Sun Jan 29 16:40:20 EST 2006


I've always found the string-building idiom

  temp_list = []
  for x in various_pieces_of_output(): 
     v = go_figure_out_some_string()
     temp_list.append(v)
  final_string = ''.join(temp_list)

completely repulsive.   As an alternative I suggest

   temp_buf = StringIO()
   for x in various_pieces_of_output(): 
      v = go_figure_out_some_string()
      temp_buf += v
   final_string = temp_buf.getvalue()

here, "temp_buf += v" is supposed to be the same as "temp_buf.write(v)".
So the suggestion is to add a __iadd__ method to StringIO and cStringIO.

Any thoughts?

Also, I wonder if it's now ok to eliminate the existing StringIO
module (make it an alias for cStringIO) now that new-style classes
permit extending cStringIO.StringIO.



More information about the Python-list mailing list