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


Groups > comp.lang.python > #10344

Re: Is this overuse a context manager?

Date 2011-07-26 11:08 -0700
From Ethan Furman <ethan@stoneleaf.us>
Subject Re: Is this overuse a context manager?
References <997tgtFqlhU1@mid.individual.net>
Newsgroups comp.lang.python
Message-ID <mailman.1504.1311702796.1164.python-list@python.org> (permalink)

Show all headers | View raw


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~

Back to comp.lang.python | Previous | NextPrevious in thread | Next 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