Why does StringIO discard its initial value?

Leif K-Brooks eurleif at ecritters.biz
Sun Apr 10 11:31:03 EDT 2005


When StringIO gets an initial value passed to its constructor, it seems 
to discard it after the first call to .write(). For instance:

 >>> from StringIO import StringIO
 >>> buffer = StringIO('foo')
 >>> buffer.getvalue()
'foo'
 >>> buffer.write('bar')
 >>> buffer.getvalue()
'bar'
 >>> buffer.write('baz')
 >>> buffer.getvalue()
'barbaz'

The obvious workaround is to call buffer.write() with the initial value 
instead of passing it to StringIO's constructor, so this issue doesn't 
bother me very much, but I'm still curious about it. Is this the 
expected behavior, and why it isn't mentioned in the docs if so?



More information about the Python-list mailing list