FAQ
Hi. The with statement is certainly nifty. The trouble is, the
*only* two documented examples how it can be used with the library
classes are file objects (which I use all the time) and thread locks
which I almost never use. Yet there are many, many classes in the
library whose use would be more elegant and readable if the with
statement could be employed. Start with the connection objects in
httplib and you can probably come up with 10 others easily. Maybe it
is the case that some of these classes have with statement support
already but I don't know it? If so, how can I know (without asking
here each time, LOL)? If not, is there work being done on that?

I am interested in this question mostly in the context of Python 2.6.

Many thanks.

Search Discussions

  • Alex23 at Feb 19, 2010 at 12:25 pm

    nobrowser wrote:
    Yet there are many, many classes in the
    library whose use would be more elegant and readable if the with
    statement could be employed. ?Start with the connection objects in
    httplib and you can probably come up with 10 others easily. ?Maybe it
    is the case that some of these classes have with statement support
    already but I don't know it? ?If so, how can I know (without asking
    here each time, LOL)? ?If not, is there work being done on that?
    If an object has __enter__ and __exit__ methods, it should work as a
    context manager.

    If you do find any such classes, submitting doc bugs or patches would
    be really handy.

    However, I'm not sure if there was any attempt to retrofit the stdlib
    with context manager supports, so if you do come up with more elegant
    approaches, please contribute them, we'll all thank you :)
  • Robert Kern at Feb 19, 2010 at 4:06 pm

    On 2010-02-19 01:18 AM, nobrowser wrote:
    Hi. The with statement is certainly nifty. The trouble is, the
    *only* two documented examples how it can be used with the library
    classes are file objects (which I use all the time) and thread locks
    which I almost never use. Yet there are many, many classes in the
    library whose use would be more elegant and readable if the with
    statement could be employed. Start with the connection objects in
    httplib and you can probably come up with 10 others easily.
    Yup. I believe that the devs wanted to adopt the new feature carefully and
    deliberately, though. They introduced it for file objects and locks first
    because those were the use cases that had been thoroughly explored in the
    development of the PEP. They held back from making every reasonable object a
    context manager in order to see how the new feature worked in the real world
    with those two use cases first.

    Now is probably a good time to start adding more sensible context managers to
    objects. I don't think there is any particular organized effort to do so, though.

    --
    Robert Kern

    "I have come to believe that the whole world is an enigma, a harmless enigma
    that is made terrible by our own mad attempt to interpret it as though it had
    an underlying truth."
    -- Umberto Eco
  • Terry Reedy at Feb 19, 2010 at 5:41 pm

    On 2/19/2010 2:18 AM, nobrowser wrote:
    Hi. The with statement is certainly nifty. The trouble is, the
    *only* two documented examples how it can be used with the library
    classes are file objects (which I use all the time) and thread locks
    which I almost never use. Yet there are many, many classes in the
    library whose use would be more elegant and readable if the with
    statement could be employed. Start with the connection objects in
    httplib and you can probably come up with 10 others easily. Maybe it
    is the case that some of these classes have with statement support
    already but I don't know it? If so, how can I know (without asking
    here each time, LOL)? If not, is there work being done on that?
    I am interested in this question mostly in the context of Python 2.6.
    2.6 is in maintenance mode only; no new features.
    2.7 will be so when released in June and effectively so in a month when
    the first beta is released.
    3.2 is a reasonable target.

    tjr
  • John Nagle at Feb 21, 2010 at 11:21 pm

    nobrowser wrote:
    Hi. The with statement is certainly nifty. The trouble is, the
    *only* two documented examples how it can be used with the library
    classes are file objects (which I use all the time) and thread locks
    which I almost never use. Yet there are many, many classes in the
    library whose use would be more elegant and readable if the with
    statement could be employed. Start with the connection objects in
    httplib and you can probably come up with 10 others easily.
    "with" is important for locks because it matters when they're
    released. It's not as important if release is just resource
    recovery. Reference counting will recover most objects when they
    go out of scope anyway.

    Don't get carried away just because a new feature is available.

    John Nagle
  • Chris Rebert at Feb 21, 2010 at 11:18 pm

    On Sun, Feb 21, 2010 at 3:21 PM, John Nagle wrote:
    nobrowser wrote:
    Hi. ?The with statement is certainly nifty. ?The trouble is, the
    *only* two documented examples how it can be used with the library
    classes are file objects (which I use all the time) and thread locks
    which I almost never use. ?Yet there are many, many classes in the
    library whose use would be more elegant and readable if the with
    statement could be employed. ?Start with the connection objects in
    httplib and you can probably come up with 10 others easily.
    ? "with" is important for locks because it matters when they're
    released. ?It's not as important if release is just resource
    recovery. ?Reference counting will recover most objects when they
    go out of scope anyway.
    If you're using CPython that is. The other implementations don't use
    refcounting and thus don't guarantee the GC will be that timely.
    So if one wants maximum portability, the `with` statement is *quite* useful.

    Not to mention there are other uses for `with` besides just freeing
    resources; e.g. setting a temporary decimal arithmetic context.

    Cheers,
    Chris

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedFeb 19, '10 at 7:18a
activeFeb 21, '10 at 11:21p
posts6
users6
websitepython.org

People

Translate

site design / logo © 2022 Grokbase