[Python-ideas] Potential PEP: with/except

Paul Ferrell pflarr at gmail.com
Tue Jan 22 17:15:04 EST 2019

On the whole chain on context managers:

I'm aware that 'exit' gets the exceptions raised, which is great in certain
situations that are specific to the object. However, the common case is
specific to the _usage_ of the object. Even if it were all just one type of
object, like opening files, there are many different situations in which I
need to handle the errors. Some may just need to be logged, some ignored,
some may need to attempt a retry, some should cause a complete failure.
Writing custom context managers for all of the different cases is the
opposite of the desired result of slightly cleaner, less redundant code.
Then consider I'm often using many different context managed objects in a
code base, often simultaneously.

This idea hit me because I keep running into the try/with/except pattern so
often, and in so many different circumstances.

On Tue, Jan 22, 2019 at 1:31 PM Michael Selik <mike at selik.org> wrote:

> On Tue, Jan 22, 2019, 12:11 PM Paul Ferrell <pflarr at gmail.com wrote:
>> I see this as the natural evolution of what 'with' is all about -
>> replacing necessary try-finally blocks with something more elegant. We just
>> didn't include the 'except' portion.
> The time machine strikes again. In fact, you can handle exceptions with a
> context manager object. Whatever you're with-ing must have a dunder exit
> method, which received any exceptions raised in the block as an argument.
> Return true and the exception is suppressed.

Paul Ferrell
pflarr at gmail.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20190122/5fc032dc/attachment.html>

More information about the Python-ideas mailing list