Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #74423

Re: Do I need to call close on the handle returned by urlopen?

References <lq0sar$r6e$1@mx1.internetia.pl>
Date 2014-07-14 10:09 -0500
Subject Re: Do I need to call close on the handle returned by urlopen?
From Skip Montanaro <skip@pobox.com>
Newsgroups comp.lang.python
Message-ID <mailman.11797.1405350590.18130.python-list@python.org> (permalink)

Show all headers | View raw


> The tutorial says that I should use "with open" to close the file
> handle properly.  The reference documentation for urlopen mentions
> that the handle returned is like a file handle but the code samples
> below do not bother to close the handle at all.  Isn’t it
> inconsistent?

I think two things might be happening here.

1. Code samples are just that. They often demonstrate usage with the
smallest amount of code possible, so that the key part of what they
are demonstrating isn't lost in a bunch of other clutter. That often
means leaving out error checking and not completely cleaning up after
themselves, especially if those bits might obscure the actual concept
being conveyed.

2. The "with" statement is (relative to many other parts of the
language) still pretty new, so it's not all that surprising that
examples in the documentation haven't all been updated to reflect the
latest recommended practice.

Skip

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Do I need to call close on the handle returned by urlopen? <krzysztof.zelechowski@syncron.com> - 2014-07-14 16:59 +0200
  Re: Do I need to call close on the handle returned by urlopen? <krzysztof.zelechowski@syncron.com> - 2014-07-14 17:08 +0200
  Re: Do I need to call close on the handle returned by urlopen? Skip Montanaro <skip@pobox.com> - 2014-07-14 10:09 -0500
  Re: Do I need to call close on the handle returned by urlopen? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-07-14 16:20 +0100

csiph-web