From 6c4c6c5718a75c9a7df23e9c7420eeae1f9097c5 Mon Sep 17 00:00:00 2001 From: Barry Warsaw Date: Fri, 10 May 2013 11:35:38 -0400 Subject: I was confused before. It's correct to not call .close() inside the with statement, but add a comment that clarifies the purpose of the code. --- Doc/library/contextlib.rst | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'Doc/library/contextlib.rst') diff --git a/Doc/library/contextlib.rst b/Doc/library/contextlib.rst index fee5067bd0..fba48f47d5 100644 --- a/Doc/library/contextlib.rst +++ b/Doc/library/contextlib.rst @@ -259,11 +259,12 @@ Functions and classes provided: with ExitStack() as stack: files = [stack.enter_context(open(fname)) for fname in filenames] - close_files = stack.pop_all().close() + # Hold onto the close method, but don't call it yet. + close_files = stack.pop_all().close # If opening any file fails, all previously opened files will be # closed automatically. If all files are opened successfully, # they will remain open even after the with statement ends. - # close_files() can then be invoked explicitly to close them all + # close_files() can then be invoked explicitly to close them all. .. method:: close() -- cgit v1.2.1