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


Groups > comp.lang.python > #10345

Re: Is this overuse a context manager?

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
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; 'context': 0.04; 'cpython': 0.05; 'received:verizon.net': 0.07; 'resource.': 0.07; 'terry': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'statement.': 0.09; 'am,': 0.13; 'wrote:': 0.15; 'aesthetic': 0.16; 'cleanly': 0.16; 'closes': 0.16; 'habit.': 0.16; 'partly': 0.16; 'reedy': 0.16; 'style,': 0.16; 'subject:overuse': 0.16; 'warn': 0.16; 'stick': 0.19; 'jan': 0.19; 'maybe': 0.22; 'header :In-Reply-To:1': 0.22; 'once.': 0.23; 'opens': 0.23; 'resources.': 0.23; 'structure': 0.23; 'code': 0.24; 'skip:l 30': 0.25; 'tests': 0.25; 'helpful': 0.26; '(and': 0.27; "i'm": 0.27; 'process,': 0.28; 'bit': 0.28; 'code,': 0.29; 'object': 0.30; 'annoying': 0.30; 'style.': 0.30; 'usable': 0.30; 'subject:?': 0.31; 'too': 0.32; 'does': 0.32; 'closing': 0.33; 'usually': 0.33; 'to:addr :python-list': 0.34; 'header:X-Complaints-To:1': 0.34; 'header :User-Agent:1': 0.34; 'there': 0.34; 'couple': 0.34; 'test': 0.34; 'things': 0.34; 'explicit': 0.35; 'forces': 0.35; 'actual': 0.35; 'file': 0.36; 'assigned': 0.36; 'some': 0.37; 'but': 0.37; 'received:org': 0.38; 'subject:: ': 0.38; 'think': 0.38; 'easier': 0.39; 'should': 0.39; 'processing': 0.39; 'header:Mime-Version:1': 0.39; 'files,': 0.39; 'help': 0.39; 'to:addr:python.org': 0.39; 'your': 0.60; 'matter': 0.61; 'believe': 0.66; 'carefully': 0.69; 'wish': 0.70; 'alternative': 0.70; 'thousands': 0.71; 'become': 0.72; 'soon': 0.73; 'quality': 0.74; 'subject:this': 0.74; 'ever,': 0.84; 'imagined': 0.84; 'involved.': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Terry Reedy <tjreedy@udel.edu>
Subject Re: Is this overuse a context manager?
Date Tue, 26 Jul 2011 14:05:50 -0400
References <997tgtFqlhU1@mid.individual.net>
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host pool-74-109-121-73.phlapa.fios.verizon.net
User-Agent Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.18) Gecko/20110616 Lightning/1.0b2 Thunderbird/3.1.11
In-Reply-To <997tgtFqlhU1@mid.individual.net>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://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 <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1505.1311703567.1164.python-list@python.org> (permalink)
Lines 49
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1311703567 news.xs4all.nl 23935 [2001:888:2000:d::a6]:41642
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:10345

Show key headers only | View raw


On 7/26/2011 9:24 AM, Neil Cerutti wrote:
> I use them all the time now, even when the resource being managed
> is used for just one line, and never need be assigned an explicit
> name. Is it good style, or annoying?

Annoying to you? or an actual or imagined audience?
>
>      with open(in_fname, newline='') as in_file:
>          folk = list(csv.DictReader(in_file))

This closes the file immediately.
>
> The obvious alternative is:
>
>      folk = list(csv.DictReader(open(in_fname, newline='')))

This happens to close the file immediately on current CPython since the 
file object is immediately deleted, but will not on some other 
implementations. If a process only opens a couple of files ever, that 
does not matter too much, although I believe the process shutdown 
procudure may warn about unclosed resources.

I sometimes do this, but know what is involved. That is partly habit.

> With the many files I have to process, I find the with statements
> create a visual structure that is helpful to track how many files
> I'm working with at once.

If processing a directory of thousands of files, I would definitely use 
the with statement.

> The existence of the context also usually forces me to think more
> carefully about how long I really need that resource.

It definitely makes it easier to find file opens in the code, should you 
wish to revise. There is also an aesthetic quality to cleanly closing 
things as soon as possible.

> But maybe I'm being a bit zeallous.

The stdlib test code has become zealous on this and other issues as it 
is intended to be usable by all conforming implementations. We need more 
zealous people like you to help with tests (and other code) ;-).

So I would stick with your style.

-- 
Terry Jan Reedy

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


Thread

Is this overuse a context manager? Neil Cerutti <neilc@norwich.edu> - 2011-07-26 13:24 +0000
  Re: Is this overuse a context manager? Ethan Furman <ethan@stoneleaf.us> - 2011-07-26 11:08 -0700
  Re: Is this overuse a context manager? Terry Reedy <tjreedy@udel.edu> - 2011-07-26 14:05 -0400

csiph-web