Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #103872
| From | Matt Wheeler <m@funkyhat.org> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: urlopen, six, and py2 |
| Date | 2016-03-02 14:35 +0000 |
| Message-ID | <mailman.103.1456929746.20602.python-list@python.org> (permalink) |
| References | <nb6rvp$cub$1@gioia.aioe.org> |
On 2 March 2016 at 14:05, Fabien <fabien.maussion@gmail.com> wrote:
> [snip]
> My question is: why does the python3 version need a "with" block while the
> python2 version doesn't? Can I skip the "with" entirely, or should I rather
> do the following:
It's not a case of "need", using the "with" construction is an added
feature, not a burden!
> from six.moves.urllib.request import urlopen
>
> try:
> with urlopen('http://www.google.com') as resp:
> _ = resp.read()
> except AttributeError:
> # python 2
> resp = urlopen('http://www.google.com')
> _ = resp.read()
This is poor practise as you aren't closing "resp".
This leaves state lying around that you don't need anymore, which is
the whole purpose of the context manager that 3 provides.
It will *usually* be cleaned up when leaving the current scope, but
won't if there is an exception thrown. Using the context manager
ensures resp is *always* cleaned up properly even if an exception is
thrown.
I agree that six should probably handle this, but in the meantime you
could use contextlib.closing ([1]) rather than rolling your own. It
even uses urlopen as an example.
If you absolutely want to roll your own then don't bother with the
context manager provided by 3 at all, just use:
resp = urlopen('http://www.google.com')
try:
_ = resp.read()
finally:
resp.close()
But as this is wordier and more fragile than using a context manager
to clean up for you, I expect you won't bother :).
[1] https://docs.python.org/2/library/contextlib.html#contextlib.closing
--
Matt Wheeler
http://funkyh.at
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
urlopen, six, and py2 Fabien <fabien.maussion@gmail.com> - 2016-03-02 15:05 +0100
Re: urlopen, six, and py2 Matt Wheeler <m@funkyhat.org> - 2016-03-02 14:35 +0000
Re: urlopen, six, and py2 Fabien <fabien.maussion@gmail.com> - 2016-03-02 16:36 +0100
Re: urlopen, six, and py2 Chris Angelico <rosuav@gmail.com> - 2016-03-03 03:17 +1100
Re: urlopen, six, and py2 Chris Angelico <rosuav@gmail.com> - 2016-03-03 01:53 +1100
csiph-web