Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d8f870d

Browse files
committed
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.
1 parent 173d410 commit d8f870d

1 file changed

Lines changed: 3 additions & 2 deletions

File tree

Doc/library/contextlib.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,11 +259,12 @@ Functions and classes provided:
259259

260260
with ExitStack() as stack:
261261
files = [stack.enter_context(open(fname)) for fname in filenames]
262-
close_files = stack.pop_all().close()
262+
# Hold onto the close method, but don't call it yet.
263+
close_files = stack.pop_all().close
263264
# If opening any file fails, all previously opened files will be
264265
# closed automatically. If all files are opened successfully,
265266
# they will remain open even after the with statement ends.
266-
# close_files() can then be invoked explicitly to close them all
267+
# close_files() can then be invoked explicitly to close them all.
267268

268269
.. method:: close()
269270

0 commit comments

Comments
 (0)