Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: 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; 'cleaned': 0.09; 'from:addr:ethan': 0.09; 'from:addr:stoneleaf.us': 0.09; 'from:name:ethan furman': 0.09; 'message-id:@stoneleaf.us': 0.09; 'received:gator410.hostgator.com': 0.09; '~ethan~': 0.09; 'wrote:': 0.15; 'denotes': 0.16; 'received:72.11': 0.16; 'received:72.11.125': 0.16; 'received:72.11.125.166': 0.16; 'style,': 0.16; 'subject:overuse': 0.16; 'object,': 0.16; 'cc:addr :python-list': 0.16; 'cc:2**0': 0.21; 'cc:no real name:2**0': 0.22; 'header:In-Reply-To:1': 0.22; 'skip:l 30': 0.25; 'suspect': 0.25; 'object': 0.30; 'cc:addr:python.org': 0.30; 'least': 0.31; 'subject:?': 0.31; 'header:User-Agent:1': 0.34; 'clearly': 0.35; 'explicit': 0.35; 'assigned': 0.36; 'some': 0.37; 'but': 0.37; 'getting': 0.38; 'subject:: ': 0.38; 'might': 0.39; 'managers': 0.62; 'received:websitewelcome.com': 0.65; 'received:184': 0.67; 'alternative': 0.70; 'subject:this': 0.74 Date: Tue, 26 Jul 2011 11:08:38 -0700 From: Ethan Furman User-Agent: Thunderbird 1.5.0.10 (Windows/20070221) MIME-Version: 1.0 To: Neil Cerutti Subject: Re: Is this overuse a context manager? References: <997tgtFqlhU1@mid.individual.net> In-Reply-To: <997tgtFqlhU1@mid.individual.net> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - gator410.hostgator.com X-AntiAbuse: Original Domain - python.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - stoneleaf.us X-BWhitelist: no X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: mail.admailinc.com ([192.168.10.136]) [72.11.125.166]:1035 X-Source-Auth: ethan+stoneleaf.us X-Email-Count: 3 X-Source-Cap: dG9idWs7dG9idWs7Z2F0b3I0MTAuaG9zdGdhdG9yLmNvbQ== Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 19 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1311702796 news.xs4all.nl 23859 [2001:888:2000:d::a6]:41266 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:10344 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? > > with open(in_fname, newline='') as in_file: > folk = list(csv.DictReader(in_file)) > > The obvious alternative is: > > folk = list(csv.DictReader(open(in_fname, newline=''))) I can see that it might take some getting used to, but I suspect the context managers are the better style: it clearly denotes the lifespan of the managed object, and provides for resource clean-up. (I know, files are already automatically cleaned up -- at least in cpython; but not every managed object will be a file.) ~Ethan~