Do I have to do StringIO.close()?

前端 未结 4 1233
旧时难觅i
旧时难觅i 2021-02-05 01:45

Some code:

import cStringIO

def f():
    buffer = cStringIO.StringIO()
    buffer.write(\'something\')
    return buffer.getvalue()

The docume

4条回答
  •  攒了一身酷
    2021-02-05 02:14

    From the source:

    class StringIO:
        ...
        def close(self):
            """Free the memory buffer.
            """
            if not self.closed:
                self.closed = True
                del self.buf, self.pos
    

    So StringIO.close just frees the memory buffer deleting references to StringIO.buf and StringIO.pos. But if self is garbage collected, its attributes will also be garbage collected, having the same effect as StringIO.close.

提交回复
热议问题