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


Groups > comp.lang.python > #10335

Is this overuse a context manager?

From Neil Cerutti <neilc@norwich.edu>
Newsgroups comp.lang.python
Subject Is this overuse a context manager?
Date 2011-07-26 13:24 +0000
Organization Norwich University
Message-ID <997tgtFqlhU1@mid.individual.net> (permalink)

Show all headers | 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