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


Groups > comp.lang.python > #10335 > unrolled thread

Is this overuse a context manager?

Started byNeil Cerutti <neilc@norwich.edu>
First post2011-07-26 13:24 +0000
Last post2011-07-26 14:05 -0400
Articles 3 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  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

#10335 — Is this overuse a context manager?

FromNeil Cerutti <neilc@norwich.edu>
Date2011-07-26 13:24 +0000
SubjectIs this overuse a context manager?
Message-ID<997tgtFqlhU1@mid.individual.net>
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

[toc] | [next] | [standalone]


#10344

FromEthan Furman <ethan@stoneleaf.us>
Date2011-07-26 11:08 -0700
Message-ID<mailman.1504.1311702796.1164.python-list@python.org>
In reply to#10335
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~

[toc] | [prev] | [next] | [standalone]


#10345

FromTerry Reedy <tjreedy@udel.edu>
Date2011-07-26 14:05 -0400
Message-ID<mailman.1505.1311703567.1164.python-list@python.org>
In reply to#10335
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web