A list of strings to be joined with ''.join
is just fine. However, if you prefer a more direct solution:
import cStringIO
class MyWriter(object):
def __init__(self, stdout):
self.stdout = stdout
self.dumps = cStringIO.StringIO()
self.final = None
def write(self, text):
self.stdout.write(smart_unicode(text).encode('cp1251'))
self.dumps.write(text)
def close(self):
self.stdout.close()
self.final = self.dumps.getvalue()
self.dumps.close()
def getvalue(self):
if self.final is not None:
return self.final
return self.dumps.getvalue()
getvalue
cannot be called on a string-io object after it's closed (closing the object makes it drop its own buffer memory) which is why I make self.final
just before that happens. Apart from the getvalue
, a string-io object is a pretty faithful implementation of the "file-like object" interface, so it often comes in handy when you just want to have some piece of code, originally designed to print
results, keep them in memory instead; but it's also a potentially neat way to "build up a string by pieces" -- just write
each piece and getvalue
when done (or at any time during the process to see what you've built up so far).
Modern Python style for this task is often to prefer the lower-abstraction approach (explicitly build a list of strings and join them up at need), but there's nothing wrong with the slightly higher-abstraction "string I/O" approach either.
(A third approach that seems a bit out of favor is to keep extend
ing an array.array
of characters, just to be comprehensive in listing these;-).