Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Is this overuse a context manager?

Reply
Thread Tools

Is this overuse a context manager?

 
 
Neil Cerutti
Guest
Posts: n/a
 
      07-26-2011
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
 
Reply With Quote
 
 
 
 
Terry Reedy
Guest
Posts: n/a
 
      07-26-2011
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

 
Reply With Quote
 
 
 
 
Ethan Furman
Guest
Posts: n/a
 
      07-26-2011
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~
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Do C++ Programmers Overuse Templates? tonytech08 C++ 103 12-19-2008 12:28 AM
Overuse of Templates Ted C++ 25 03-26-2006 08:58 AM
Re: Overuse of templates Ted C++ 0 03-25-2006 02:56 AM
Opinion) Overuse of symbolic constants Sandeep Sharma C++ 35 05-03-2004 05:32 PM
Opinion) Overuse of symbolic constants Sandeep Sharma C Programming 59 05-03-2004 05:32 PM



Advertisments