Is this overuse a context manager?

Neil Cerutti neilc at
Tue Jul 26 15:24:45 CEST 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

More information about the Python-list mailing list