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?

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <skip.montanaro@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'tutorial': 0.03; 'smallest': 0.07; 'bits': 0.09; 'mentions': 0.09; 'properly.': 0.09; 'themselves,': 0.09; 'cc:addr:python-list': 0.11; '"with"': 0.16; '(relative': 0.16; 'from:addr:pobox.com': 0.16; 'from:addr:skip': 0.16; 'language)': 0.16; 'surprising': 0.16; 'demonstrate': 0.16; 'all.': 0.16; 'sender:addr:gmail.com': 0.17; 'possible,': 0.19; 'subject:need': 0.19; 'examples': 0.20; 'cc:addr:python.org': 0.22; 'error': 0.23; 'new,': 0.24; 'skip': 0.24; "haven't": 0.24; 'cc:2**0': 0.24; 'header:In-Reply-To:1': 0.27; 'returned': 0.30; 'statement': 0.30; 'especially': 0.30; 'message-id:@mail.gmail.com': 0.30; 'code': 0.31; 'that.': 0.31; 'bunch': 0.31; 'obscure': 0.31; 'file': 0.32; 'says': 0.33; 'checking': 0.33; 'actual': 0.34; 'updated': 0.34; 'subject:the': 0.34; 'but': 0.35; 'received:google.com': 0.35; 'reflect': 0.36; 'subject:?': 0.36; 'should': 0.36; 'two': 0.37; 'being': 0.38; 'handle': 0.38; 'lost': 0.61; 'latest': 0.67; 'close': 0.67; 'subject:handle': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:in-reply-to:references:date:message-id:subject :from:to:cc:content-type:content-transfer-encoding; bh=9TiJql9GoS3nUHR2jC1qviANTK0O9kvubirVqiLqiNs=; b=INntodmugEYGOolGz4M6dZqtlKEWiziqB4JeIYdZQbEQS8uG+U/V/yiQJvdgZOByf6 ZYN6O2YGSEirL4MdEK0PJfAD5IsME+4yLt9SG4dgheBnGnhDpmbqITfr6Z71JlOv8vtZ zh3R2ESXmOYKrqVhpF/irUpjr/aFpW2khs/GRAnZMwtQAaJ4HqPppXPCUR5nRfTf9Ylb hARo8ZOKMFzTaAENKcC6XKMHlUgL2TmJoRCp2TKi34O+LFPDb6Us2kS1tysw7KCaQtYs qZGyBHc8KvzmCvX5W0wn2J6knkVJCO33EmqmfiBM9mHrfpuWglgO3anEZLDKH3hZ0n9s Q5Jw==
MIME-Version 1.0
X-Received by 10.43.7.73 with SMTP id on9mr22271403icb.51.1405350582097; Mon, 14 Jul 2014 08:09:42 -0700 (PDT)
Sender skip.montanaro@gmail.com
In-Reply-To <lq0sar$r6e$1@mx1.internetia.pl>
References <lq0sar$r6e$1@mx1.internetia.pl>
Date Mon, 14 Jul 2014 10:09:42 -0500
X-Google-Sender-Auth WQ-SXP_19w52iJHoR4GjjHxeQ_0
Subject Re: Do I need to call close on the handle returned by urlopen?
From Skip Montanaro <skip@pobox.com>
To krzysztof.zelechowski@syncron.com
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding quoted-printable
Cc Python <python-list@python.org>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.11797.1405350590.18130.python-list@python.org> (permalink)
Lines 21
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1405350590 news.xs4all.nl 2936 [2001:888:2000:d::a6]:45051
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:74423

Show key headers only | 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