From: nobrowser on
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.
From: alex23 on
nobrowser <nobrow...(a)gmail.com> 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 :)
From: Robert Kern on
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

From: Terry Reedy on
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

From: John Nagle on
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