On Wed, Jun 16, 2021 at 10:51 AM Soni L. <fakedme+py@gmail.com> wrote:
>
> Sometimes it would be useful to be able to write:
>
> def foo():
> try: return thing()
> except ValueError;
> try: return otherthing()
> except ValueError;
> try: return yetotherthing()
> except ValueError;
> if shouldraise(): raise
>
> But currently this needs to be written like so:
>
> def foo():
> try: return thing()
> except ValueError:
> try: return otherthing()
> except ValueError:
> try: return yetotherthing()
> except ValueError:
> if shouldraise(): raise
>
> Look at all that unnecessary indentation! Would be nice to get rid of it.
Dangerous idea - my first interpretation of that syntax was that it
would be equivalent to "except ValueError: pass", which would be very
confusing (it's subtly different in your example with return, and
drastically different in other cases).
Are you doing this sort of thing a lot? And if you are, do you
actually need/want the exception chaining that comes from burying more
and more code into the except clauses? I know this is just a trivial
example, but I'd be looking to see if it can be done with a loop
instead.
def foo():
for func in (thing, otherthing, yetotherthing):
try: return func()
except ValueError: pass
or something like that.
ChrisA
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-leave@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/CJCSPO4N3RBGMXDFPT7HHIFROM4BZLN6/
Code of Conduct: http://python.org/psf/codeofconduct/