On Fri, Oct 16, 2020 at 02:14:27AM +0100, Rob Cliffe via Python-ideas 
wrote:

> >You mean like.... a goto statement? I'm not sure what a "pseudo-loop"
> >is, other than a way to use break as goto.
> >
> >ChrisA
>
> Is that bad?  As I tried to explain, sometimes a process may need to be 
> abandoned at multiple points instead of running to completion (it may 
> succeed early or fail early).

The usual way to handle that is by returning from a function, as you 
point out.


> If the process were in a dedicated function, this would be done by 
> multiple return statements, and nobody would raise an eyebrow.

There are a (very small?) number of people who would raise an eyebrow, 
as they insist on a strict view of "Single entry, single exit" and would 
argue against both break and early return from a function.

http://wiki.c2.com/?SingleFunctionExitPoint

I'm not one of them :-)


> However, sometimes it is not convenient to hive the process off into its 
> own function (it might necessitate passing and receiving long 
> hard-to-maintain lists of what used to be local variables).

Hmmm, well, I won't categorically say you *must* use a function, but I 
will say that using a function is probably the best solution.

If you nest your function inside the major function, you don't even need 
to pass arguments:


    def function(a, b, c, d, e):
        # More local variables:
        f, g, h = 1, 2, 3
        
        def inner():
            # all locals a...h are visible in here
            return result

        thing = inner()


so you may be able to move your processing to an inner function, without 
needing to explicit pass any arguments to it, and make use of return, 
rather than adding a loop so you can use break.


-- 
Steve
_______________________________________________
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/DB2MGO24CVMXVJBMLYXKYJ4LCBVSDM66/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to