==cStringIO 模塊== ``cStringIO`` 是一個可選的模塊, 是 ``StringIO`` 的更快速實現. 它的工作方式和 ``StringIO`` 基本相同, 但是它不可以被繼承. [Example 2-11 #eg-2-11] 展示了 ``cStringIO`` 的用法, 另參考前一節. ====Example 2-11. 使用 cStringIO 模塊====[eg-2-11] ``` 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() *B*That man is depriving a village somewhere of a computer scientist.*b* ``` 為了讓你的代碼盡可能快, 但同時保證兼容低版本的 Python ,你可以使用一個小技巧在 ``cStringIO`` 不可用時啟用 ``StringIO`` 模塊, 如 [Example 2-12 #eg-2-12] 所示. ====Example 2-12. 后退至 StringIO====[eg-2-12] ``` File: cstringio-example-2.py try: import cStringIO StringIO = cStringIO except ImportError: import StringIO print StringIO *B*<module 'StringIO' (built-in)>*b* ```