[Python-Dev] Tutorial: Brief Introduction to the Standard Libary

François Pinard pinard at iro.umontreal.ca
Thu Nov 27 10:26:49 EST 2003

[Alex Martelli]

> cStringIO -- I've noticed most newbies find it more natural to "write to
> a cStringIO.StringIO pseudofile as they go" then getvalue, rather than
> append'ing to a list of strings then ''.join .

I do not doubt that cStringIO is useful to know, and a tutorial could
throw a short glimpse here about why the `c' prefix and speed issues.

For a newcomer, here might be a good opportunity for illustrating one
surprising capability of Python for those coming from other languages,
which is using bound methods as "first-class" objects.  Like:

    fragments = []
    write = fragments.append
    <code using `write' above>
    result = ''.join(fragments)

I think this approach is not much more difficult than `StringIO', not so
bad efficiency-wise, but likely more fruitful about developing Python
useful understanding and abilities.  A tutorial might also show that the
said `write' could be given and received in functions, which do not have
to "know" if they are writing to a file, or in-memory fragments.

François Pinard   http://www.iro.umontreal.ca/~pinard

More information about the Python-Dev mailing list