Is this overuse a context manager?

Discussion in 'Python' started by Neil Cerutti, Jul 26, 2011.

  1. Neil Cerutti

    Neil Cerutti Guest

    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
    Neil Cerutti, Jul 26, 2011
    #1
    1. Advertising

  2. Neil Cerutti

    Terry Reedy Guest

    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
    Terry Reedy, Jul 26, 2011
    #2
    1. Advertising

  3. Neil Cerutti

    Ethan Furman Guest

    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~
    Ethan Furman, Jul 26, 2011
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sandeep Sharma

    Opinion) Overuse of symbolic constants

    Sandeep Sharma, Apr 21, 2004, in forum: C++
    Replies:
    35
    Views:
    866
    Michael Wojcik
    May 3, 2004
  2. Sandeep Sharma

    Opinion) Overuse of symbolic constants

    Sandeep Sharma, Apr 21, 2004, in forum: C Programming
    Replies:
    59
    Views:
    1,265
    Michael Wojcik
    May 3, 2004
  3. Ted

    Overuse of Templates

    Ted, Mar 25, 2006, in forum: C++
    Replies:
    25
    Views:
    745
    Ian Collins
    Mar 26, 2006
  4. Ted
    Replies:
    0
    Views:
    370
  5. tonytech08
    Replies:
    103
    Views:
    2,974
    Jerry Coffin
    Dec 19, 2008
Loading...

Share This Page