On 2021-06-15 10:13 p.m., Joao S. O. Bueno wrote: > Sorry - personally I think this is absolutely ugly :-) So I will > bikeshed. > > If this thread even go ahead - since the idea is not that bad, maybe > allowing `try` on the same line? > Then it would be inline with `elif` - but still structured "English like" > > try: > statement > except ValueError try: > statement2 > except TypeError: > ... > > I had sometimes needed 2 and maybe up to 3 levels of this, nothing > 'blocker', but maybe, > just maybe, it would not be bad. > However, in real life, usually one wants to put more > statements on the `except` clause than a bare nested try block. > (logging, etc...)
For sure. The point is to turn the "rest of the function" into the except block. Note that the try block would have to end with a return, and no finally block would be allowed. > > > > > On Tue, 15 Jun 2021 at 21:58, Chris Angelico <[email protected] > <mailto:[email protected]>> wrote: > > On Wed, Jun 16, 2021 at 10:51 AM Soni L. <[email protected] > <mailto:fakedme%[email protected]>> 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 -- [email protected] > <mailto:[email protected]> > To unsubscribe send an email to [email protected] > <mailto:[email protected]> > https://mail.python.org/mailman3/lists/python-ideas.python.org/ > <https://mail.python.org/mailman3/lists/python-ideas.python.org/> > Message archived at > > https://mail.python.org/archives/list/[email protected]/message/CJCSPO4N3RBGMXDFPT7HHIFROM4BZLN6/ > > <https://mail.python.org/archives/list/[email protected]/message/CJCSPO4N3RBGMXDFPT7HHIFROM4BZLN6/> > Code of Conduct: http://python.org/psf/codeofconduct/ > <http://python.org/psf/codeofconduct/> > > > _______________________________________________ > Python-ideas mailing list -- [email protected] > To unsubscribe send an email to [email protected] > https://mail.python.org/mailman3/lists/python-ideas.python.org/ > Message archived at > https://mail.python.org/archives/list/[email protected]/message/ISYQKXGEVJOA3SY3SKJ7EZ4TUIS2ZGBC/ > Code of Conduct: http://python.org/psf/codeofconduct/
_______________________________________________ Python-ideas mailing list -- [email protected] To unsubscribe send an email to [email protected] https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/[email protected]/message/NVHMGSSJ6CG4EXEFUI34CHUUFLBTLXBV/ Code of Conduct: http://python.org/psf/codeofconduct/
