with statement and standard library
robert.kern at gmail.com
Fri Feb 19 17:06:10 CET 2010
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.
"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
More information about the Python-list