with statement and standard library

alex23 wuwei23 at gmail.com
Fri Feb 19 13:25:14 CET 2010

nobrowser <nobrow... at 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 :)

More information about the Python-list mailing list