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


Groups > comp.lang.python > #10335

Is this overuse a context manager?

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!news.musoftware.de!wum.musoftware.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From Neil Cerutti <neilc@norwich.edu>
Newsgroups comp.lang.python
Subject Is this overuse a context manager?
Date 26 Jul 2011 13:24:45 GMT
Organization Norwich University
Lines 22
Message-ID <997tgtFqlhU1@mid.individual.net> (permalink)
Mime-Version 1.0
Content-Type text/plain; charset=us-ascii
Content-Transfer-Encoding 7bit
X-Trace individual.net JbvzjPdVnq5jgaf2vKitjA5Ob4pcqw9LL+8WmbirfPAwt0bIYK
Cancel-Lock sha1:1PdIdgWFnLpkV26v/p+EGGzKLig=
User-Agent slrn/0.9.9p1/mm/ao (Win32)
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:10335

Show key headers only | View raw


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='')))

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.

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

But maybe I'm being a bit zeallous.

-- 
Neil Cerutti

Back to comp.lang.python | Previous | NextNext 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