Why does StringIO discard its initial value?

Raymond Hettinger vze4rx4y at verizon.net
Tue Apr 12 23:41:25 EDT 2005


[David Fraser]
> Others may find this helpful ; it's a pure Python wrapper for cStringIO
> that makes it behave like StringIO in not having initialized objects
> readonly. Would it be an idea to extend cStringIO like this in the
> standard library? It shouldn't lose performance if used like a standard
> cStringIO, but it prevents frustration :-)

IMO, that would be a step backwards.  Initializing the object and then
writing to it is not a good practice.  The cStringIOAPI needs to be as
file-like as possible.  With files, we create an emtpy object and then
starting writing (the append mode for existing files is a different story).
Good code ought to maintain that parallelism so that it is easier to
substitute a real file for a writeable cStringIO object.

This whole thread (except for the documentation issue which has been
fixed) is about fighting the API rather than letting it be a guide to good
code.

If there were something wrong with the API, Guido would have long
since fired up the time machine and changed the timeline so that all
would be as right as rain ;-)


Raymond Hettinger





More information about the Python-list mailing list