[Python-ideas] Re: Compound with .. else statement

2020-09-09 Thread axelheider
Hi, I was just about to make exactly the suggestion. Seem like a nice idea to have this, because it simplifies error handling a lot. Axel ___ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@pytho

[Python-ideas] Re: Compound with .. else statement

2020-04-03 Thread Jimmy Thrasibule
> but we don't need special syntax for "else" on a for or while loop (or try > block) either, you could always set a sentinel for those too. > which to me is a case for adding else to a "with" block as well, for all the > same reasons it's there for the other block construct. That's part of my o

[Python-ideas] Re: Compound with .. else statement

2020-03-31 Thread Christopher Barker
On Tue, Mar 31, 2020 at 2:41 AM Serhiy Storchaka wrote: > 31.03.20 01:32, Christopher Barker пише: > > 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

[Python-ideas] Re: Compound with .. else statement

2020-03-31 Thread Andrew Barnert via Python-ideas
On Mar 31, 2020, at 03:06, Jimmy Thrasibule wrote: > >  >> >> But if you use a context manager which silences the exception, like >> contextlib.suppress() or unittest.TestCase.assertRaises(), it is easy to >> do too. >> >> was_not_raised = False >> with my_context(): >> do_so

[Python-ideas] Re: Compound with .. else statement

2020-03-31 Thread Jimmy Thrasibule
> But if you use a context manager which silences the exception, like > contextlib.suppress() or unittest.TestCase.assertRaises(), it is easy to > do too. > > was_not_raised = False > with my_context(): >do_something_sensitive() >was_not_raised = True > if was

[Python-ideas] Re: Compound with .. else statement

2020-03-31 Thread Serhiy Storchaka
31.03.20 01:32, Christopher Barker пише: 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 informati

[Python-ideas] Re: Compound with .. else statement

2020-03-30 Thread Jimmy Thrasibule
> > Perhaps you could use try/finally: > > try: > prepare() > do_something_sensitive() > finally: > cleanup() > Well I actually would like to run the else block in case an exception did occurred. Let me provide an example from my use case which is the management of

[Python-ideas] Re: Compound with .. else statement

2020-03-30 Thread Christopher Barker
On Mon, Mar 30, 2020 at 3:19 PM Serhiy Storchaka 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. Yo

[Python-ideas] Re: Compound with .. else statement

2020-03-30 Thread Serhiy Storchaka
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()

[Python-ideas] Re: Compound with .. else statement

2020-03-30 Thread Dan Sommers
On Mon, 30 Mar 2020 23:27:19 +0200 Jimmy Thrasibule wrote: > Now imagine that in my `try .. except` block I have some heavy setup > to do before `do_something_sensitive()` and some heavy cleanup when > the exception occurs. > I'd like my context manager to do the preparation work, execute the >