[Python-ideas] Re: else without except

2023-08-01 Thread Celelibi
2023-06-30 19:54 UTC+02:00, MRAB :
> On 2023-06-30 14:55, Daniel Walker wrote:
>> As most of you probably know, you can use else with try blocks:
>>
>> try:
>>  do_stuff()
>> except SomeExceptionClass:
>> handle_error()
>> else:
>> no_error_occurred()
>>
>> Here, no_error_occurred will only be called if do_stuff() didn't raise
>> an exception.
>>
>> However, the following is invalid syntax:
>>
>> try:
>> do_stuff()
>> else:
>> no_error_occurred()
>>
>> Now you might say that this isn't needed as you can achieve the same
>> result with
>>
>> do_stuff()
>> no_error_occurred()
>>
>> However, what if I want to use finally as well:
>>
>> try:
>> do_stuff()
>> else:
>> no_error_occurred()
>> finally:
>> cleanup()
>>
>> and I need no_error_occurred to be called before cleanup?  For my actual
>> use case, I've done
>>
>> try:
>> do_stuff()
>> except Exception:
>>  raise
>> else:
>> no_error_occurred()
>> finally:
>> cleanup()
>>
>> This seems very non-Pythonic.  Is there a reason why else without except
>> has to be invalid syntax?
>>
> What would be the difference between
>
>  try:
> do_stuff()
>  else:
> no_error_occurred()
>  finally:
> cleanup()
>
> and
>
>  try:
> do_stuff()
> no_error_occurred()
>  finally:
> cleanup()
>
> ?
>

One could argue that it's conceptually not the same although the
actual execution is the same.

In the first case you intend to only catch the exceptions generated by
do_stuff().
In the second case, you intend to catch those generated by do_stuff()
and those generated by no_error_occurred().

If later on an "except" is added, the developper doing the
modification should be reminded to move the call to
no_error_occurred() into an "else". With real-world non-trivial code,
it might not be so simple to see.

I'm not an expert, but I actually see no downside to having an "else"
without an "except".


Best regards,
Celelibi
___
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/ROEHA53I2LV6PIR32ZAGAWTK6SEV5CBJ/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: else without except

2023-08-01 Thread Chris Angelico
On Wed, 2 Aug 2023 at 02:02, Celelibi  wrote:
> If later on an "except" is added, the developper doing the
> modification should be reminded to move the call to
> no_error_occurred() into an "else". With real-world non-trivial code,
> it might not be so simple to see.
>

Can you give us an example of real-world non-trivial code that would
benefit from this?

ChrisA
___
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/XOWJDT2SX4CSSWJBFWGJO5V6I7OWX3YL/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: else without except

2023-08-01 Thread Mitch
Is this a relevant argument (either way) here?

While I appreciate considering the applicability of the argument to
existing code is generally a good thing, I'm not sure that it makes sense
for cases like this where a logical outcome seems to be missing. If you can
try/finally and then implicitly pass both the generic except and else
clauses, and you can try/except/finally and then implicitly pass the else
clause, why shouldn't the construction be logically consistent to cover the
other option that is try/else/finally and then implicitly pass the generic
except clause?

And, for what it's worth, finding current scenarios for something like this
seems heavily biased by the fact that such a construction doesn't currently
exist and so code will be written to explicitly avoid it... Or just ignore
being necessarily precise, as is often the case with implementations of
try/except that I've seen (see the prior discussion between MRAB and
Celelibi about overinclusion in the try block).

I'll ask the same question as OP: "Is there a *reason* why else without
except has to be invalid syntax?"



On Tue, Aug 1, 2023 at 10:18 AM Chris Angelico  wrote:

> On Wed, 2 Aug 2023 at 02:02, Celelibi  wrote:
> > If later on an "except" is added, the developper doing the
> > modification should be reminded to move the call to
> > no_error_occurred() into an "else". With real-world non-trivial code,
> > it might not be so simple to see.
> >
>
> Can you give us an example of real-world non-trivial code that would
> benefit from this?
>
> ChrisA
> ___
> 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/XOWJDT2SX4CSSWJBFWGJO5V6I7OWX3YL/
> Code of Conduct: http://python.org/psf/codeofconduct/
>
___
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/BTTLG7SJPCSVCQH4GLRRGIJHHHXUTXOF/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: else without except

2023-08-01 Thread Mitch
Reading on mobile I missed that you were referencing Celelibi's answer and
not the thread generally [facepalm]. My apologies for that.

I've been previously bitten (multiple times) by code where a developer has
used a try/except to catch errors from one of several actions in a try
block, but a bug arose when one of the other functions in that block raised
the same error in an edge case. (I'm unfortunately not able to share that
code right now, but it is used by more than just a small team.) This could
have been fixed by better use of the else clause, instead of just
try/except, so I'd support ways to make the else clause more functional.

I might be persuaded that it feels nonsensical to have an "else" if there
is no "except", however... (because semantically if we haven't excepted
anything, there is nothing for "else" to stand against).


On Tue, Aug 1, 2023 at 11:59 AM Mitch  wrote:

> Is this a relevant argument (either way) here?
>
> While I appreciate considering the applicability of the argument to
> existing code is generally a good thing, I'm not sure that it makes sense
> for cases like this where a logical outcome seems to be missing. If you can
> try/finally and then implicitly pass both the generic except and else
> clauses, and you can try/except/finally and then implicitly pass the else
> clause, why shouldn't the construction be logically consistent to cover the
> other option that is try/else/finally and then implicitly pass the generic
> except clause?
>
> And, for what it's worth, finding current scenarios for something like
> this seems heavily biased by the fact that such a construction doesn't
> currently exist and so code will be written to explicitly avoid it... Or
> just ignore being necessarily precise, as is often the case with
> implementations of try/except that I've seen (see the prior discussion
> between MRAB and Celelibi about overinclusion in the try block).
>
> I'll ask the same question as OP: "Is there a *reason* why else without
> except has to be invalid syntax?"
>
>
>
> On Tue, Aug 1, 2023 at 10:18 AM Chris Angelico  wrote:
>
>> On Wed, 2 Aug 2023 at 02:02, Celelibi  wrote:
>> > If later on an "except" is added, the developper doing the
>> > modification should be reminded to move the call to
>> > no_error_occurred() into an "else". With real-world non-trivial code,
>> > it might not be so simple to see.
>> >
>>
>> Can you give us an example of real-world non-trivial code that would
>> benefit from this?
>>
>> ChrisA
>> ___
>> 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/XOWJDT2SX4CSSWJBFWGJO5V6I7OWX3YL/
>> Code of Conduct: http://python.org/psf/codeofconduct/
>>
>
___
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/PRMHHGBO5ZEVNSICEGO5M2KXVNVP2ZVS/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: else without except

2023-08-01 Thread Ronald Oussoren via Python-ideas


> On 1 Aug 2023, at 19:59, Mitch  wrote:
> 
> Is this a relevant argument (either way) here? 

Adding features to the language always has a cost in that it slightly 
complicates the language and makes it harder to teach. Because of that the bar 
for adding features is high.

Showing how a new feature would improve realistic code patterns helps to defend 
to proposal.

Ronald

—

Twitter / micro.blog: @ronaldoussoren
Mastodon: @ron...@blog.ronaldoussoren.net.
Blog: https://blog.ronaldoussoren.net/

___
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/MDHXGYT55K6EMFJZ5BXWWPKPB476QXVX/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: else without except

2023-08-01 Thread Chris Angelico
On Wed, 2 Aug 2023 at 04:03, Mitch  wrote:
> I'll ask the same question as OP: "Is there a reason why else without except 
> has to be invalid syntax?"
>

A better question is: "Is there a reason why else without except
should be valid syntax?"

ChrisA
___
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/BMYQ2U5EX7XH46DAUQTGC373TSU5IGVC/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: else without except

2023-08-01 Thread Mitch
Hmm, ok, I can work with that :)

If I were writing Python right now, I would argue that if you're going to
build this try/except/else/finally construct, each of the three result
clauses represent potential outcomes. The fail case, the success case, and
the no-matter-what case. And then, you should be consistent about what
happens when any of the three are omitted. If `except` is omitted, then
catch the generic exception and do nothing and exit. If `else` is omitted,
do nothing and exit. And finally, if `finally` is omitted, (surprise!) do
nothing and exit. Then, with this as a guiding principle, a user can choose
to omit any or all of those actions that they desire, except where it is
unreasonable to omit that combination. `try` by itself just silently
catches errors and does nothing, and so it seems justifiably invalid as an
abuse of the construct. Same principle actually applies to `try/else` in my
mind, since it is functionally no different. But the `try/else/finally`
statement actually has meaning: try something, do something if it succeeds,
and then do something else regardless of what happens. I don't see an
obvious reason why it doesn't exist (except perhaps the notion that an else
without an except is just semantically odd), and in fact it comes to me as
something of a surprise that it's disallowed.

All said, I'm obviously not writing Python today—and I also don't maintain
it—so I fully appreciate my own dreams of logical consistency above all
else are unlikely to be an opinion shared among the developers. I do
appreciate that this language is not changing nonstop beneath my feet, so
the inertia is frustratingly welcome.

-Mitch

On Tue, Aug 1, 2023 at 3:06 PM Chris Angelico  wrote:

> On Wed, 2 Aug 2023 at 04:03, Mitch  wrote:
> > I'll ask the same question as OP: "Is there a reason why else without
> except has to be invalid syntax?"
> >
>
> A better question is: "Is there a reason why else without except
> should be valid syntax?"
>
> ChrisA
> ___
> 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/BMYQ2U5EX7XH46DAUQTGC373TSU5IGVC/
> Code of Conduct: http://python.org/psf/codeofconduct/
>
___
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/SLNROGWWZ4YHYL4QI6JUGFKMIERMVKE2/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: else without except

2023-08-01 Thread Chris Angelico
On Wed, 2 Aug 2023 at 08:22, Mitch  wrote:
>
> If `except` is omitted, then catch the generic exception and do nothing and 
> exit.
>

Not sure what you mean here. If you have a try-finally with no except
clause, no exceptions will be caught; the try will be run, then the
finally, and then you'll see any exception from the try block.

Perhaps what you're intending is for an omitted except clause to
behave like "except: raise"? Because that would probably have most of
the effect of the proposed "try-else", without a dangling else.

And I'd still like to see an actual real-world (or "near-real-world")
example that would benefit from this. Would adding "except: raise" be
a problem?

ChrisA
___
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/OK5LMB7PAF2RRHQE7FHE2E7X6CYIFUDD/
Code of Conduct: http://python.org/psf/codeofconduct/