On Mon, Mar 30, 2020 at 3:19 PM Serhiy Storchaka <storch...@gmail.com>
wrote:

> 31.03.20 00:27, Jimmy Thrasibule пише:
> > In my situation, I would like to mix the `with` statement with `else`.
> > In this case I would like that if no exception is raised within the
> > `with` to run the `else` part.
>
> It is easy. You do not need "else".
>
> with my_context():
>       do_something_sensitive()
> print("We're all safe.")


In case Serhiy's answer wasn't clear: context managers can be written to
handle exceptions (within their context) in any way you see fit.

that is: the method:

__exit__(self, exc_type, exc_value, exc_traceback):

get the exception, and information about it, of one is raised, so you can
handle it anyway you want.

-CHB



> _______________________________________________
> Python-ideas mailing list -- python-ideas@python.org
> To unsubscribe send an email to python-ideas-le...@python.org
> https://mail.python.org/mailman3/lists/python-ideas.python.org/
> Message archived at
> https://mail.python.org/archives/list/python-ideas@python.org/message/M7HVLCGQ6REMHKGZVZPIYTJGLN6WKN5F/
> Code of Conduct: http://python.org/psf/codeofconduct/
>


-- 
Christopher Barker, PhD

Python Language Consulting
  - Teaching
  - Scientific Software Development
  - Desktop GUI and Web Development
  - wxPython, numpy, scipy, Cython
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/7SXSGIENYXWKWTJFC7LELWT3XVH3EUOV/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to