<div dir="ltr"><div>On the whole chain on context managers:</div><div><br></div><div>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.</div><div><br></div><div>This idea hit me because I keep running into the try/with/except pattern so often, and in so many different circumstances. <br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, Jan 22, 2019 at 1:31 PM Michael Selik <<a href="mailto:mike@selik.org">mike@selik.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="auto"><div><div class="gmail_quote"><div dir="ltr">On Tue, Jan 22, 2019, 12:11 PM Paul Ferrell <<a href="mailto:pflarr@gmail.com" target="_blank">pflarr@gmail.com</a> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">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.<br></blockquote></div></div><div dir="auto"><br></div><div dir="auto">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.</div><div dir="auto"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"></blockquote></div></div></div>
</blockquote></div><br clear="all"><br>-- <br><div dir="ltr" class="gmail_signature"><div dir="ltr"><div><div dir="ltr">Paul Ferrell<div><a href="mailto:pflarr@gmail.com" target="_blank">pflarr@gmail.com</a></div></div></div></div></div>