I am interested in this question mostly in the context of Python 2.6.
Many thanks.
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 :)
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
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
"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
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
--
http://blog.rebertia.com