The cStringIO module
(Optional). This module contains a faster implementation of the StringIO module. It works exactly like StringIO, but it cannot be subclassed.
Example: Using the cStringIO module
# File: cstringio-example-1.py import cStringIO MESSAGE = "That man is depriving a village somewhere of a computer scientist." file = cStringIO.StringIO(MESSAGE) print file.read()
$ python cstringio-example-1.py That man is depriving a village somewhere of a computer scientist.
To make your code as fast as possible, but also robust enough to run on older Python installations, you can fall back on the StringIO module if cStringIO is not available:
Example: Falling back on the StringIO module
# File: cstringio-example-2.py try: import cStringIO StringIO = cStringIO except ImportError: import StringIO print StringIO
$ python cstringio-example-2.py <module 'cStringIO' (built-in)>