On Wed, 2 Mar 2022 at 10:32, Rob Cliffe via Python-ideas <[email protected]> wrote: > > > > On 01/03/2022 22:25, Chris Angelico wrote: > > On Wed, 2 Mar 2022 at 09:24, Steven D'Aprano <[email protected]> wrote: > >> On Tue, Mar 01, 2022 at 04:04:31PM +0000, Rob Cliffe via Python-ideas > >> wrote: > >> > >>> I have use cases for "do exactly once". > >>> Basically a sequence of actions which can be broken off (when something > >>> goes wrong and the whole process should be aborted, or when something > >>> succeeds and there is no need to try alternatives) at various points > >>> with `break`. > >> class MyBreak(Exception): > >> pass > >> > >> try: > >> do_this() > >> if condition: raise MyBreak > >> do_that() > >> if condition: raise MyBreak > >> do_next_step() > >> if condition: raise MyBreak > >> do_last_step() > >> except MyBreak: > >> pass > >> > > All this is assuming that you can't refactor it into a function and > > 'return' each time. That's also a viable option, where applicable. > Yes, it is a viable (even preferable) option when applicable. > But there are times when it is not applicable, or is very inconvenient. > E.g. when a long list of local variables have to be turned into long > verbose error-prone and hard-to maintain lists of parameters and return > values (if the code is factored off into a function).
Inner functions are spectacular for that :) ChrisA _______________________________________________ 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/QJWNWGK6W2MB6NQO3ZD6GRZLC5J43XC2/ Code of Conduct: http://python.org/psf/codeofconduct/
