On Thu, 13 Apr 2017 01:42:01 +0200, Mikhail V wrote:

> On 12 April 2017 at 02:44, Nathan Ernst <nathan.er...@gmail.com> wrote:
>> goto is a misunderstood and much misaligned creature. It is a very
>> useful feature, but like nearly any programming construct can be
>> abused.
>>
>> Constructs like 'break', 'continue' or 'next' in languages like Python
>> or C/C++ are goto's with implied labels.
>>
>> As Mikhail said, goto's can be great to break out of nested loops (only
>> a handful of languages support named 'breaks').
>>
>> So, instead of:
>> bool found = false;
>> for (int i = 0; i = ...; ++i)
>> {
>>  for (int h = 0; h = ...; ++h)
>>  {
>>    if (some_condition)
>>      found = true;
>>  }
>>  if (found) break;
>> }
>>
>> You can have:
>>
>> for (int i = 0; i = ...; ++i)
>> {
>>  for (int h = 0; h = ...; ++h)
>>  {
>>    if (some_condition)
>>      goto found;
>>  }
>> }
>> // not found
>>
>> found:
>> // handle found
>>
>> The second is better for a number of reasons: it's clearer. It has
>> fewer variables (smaller stack), it has fewer branches (better for the
>> CPU's branch prediction), and it has fewer instructions (better for CPU
>> instruction cache).  This is a trivial, contrived example, but I've
>> seen more than 4x nested loops using an exit flag like this (at every
>> level of the loops) that could have been replaced with a lot less code.
>>
>> People need to stop drinking "X is considered harmful." Even Dijkstra
>> later lamented that his "Goto considered harmful" paper was
>> misinterpreted and misrepresented as advocating that goto should never
>> be used.
>>
>> Additionally, I'd recommend everyone read '"Considered Harmful" Essays
>> Considered Harmful': http://meyerweb.com/eric/comment/chech.html
>>
>> Regards,
>> Nate
>>
>>
> 
> 
> Here are recent related discussions about exiting from nested loops (at
> least seems to be so):
> https://mail.python.org/pipermail/python-ideas/2017-March/044932.html
> https://mail.python.org/pipermail/python-ideas/2017-March/045049.html
> 
> I personally have difficulties to fully understand some of the examples
> given in those proposals, namely that proposals were thought for
> other(?) purposes also,
> not only breaking nested loops.
> 
> At a first glance it seems to me that "goto" would solve the nested
> breaking problem and in a nice flexible way.
> (correct  me if I am wrong, probably I'm missing something)
> 
> So besides nested loops, in my practice I had occasions when I would
> want to use "goto".
> It would be fair to say that those occasions are not so common,
> but I'll try to give an example. Say, there is some pure procedural
> processing for some simple task and I don't want to make the script
> heavy and just care for the clarity:
> 
> ===========
> Log = ""
> S = "lorem ipsum"
> 
> for s in S:
>     if s == "i" :
>         message = "found on stage 1"
>         goto .output
> 
> S = S + " hello world"
> for s in S:
>     if s == "d" :
>         message = "found on stage 2"
>         goto .output
> 
> print "not found"
> print "S = ", S goto .exit
> 
> .output print message Log = Log + message
> 
> .exit:
> print "exiting"
> =============
> 
> For me it looks clear and I'd say easy to comprehend,
> Main critic would be obviously that it is not a good, *scalable
> application*, but quite often I don't even have this in mind, and just
> want to express a step-by-step direct instructions.
> In this case sticking any "def():.." inside the script does not make any
> sense for me. Simplicity here reflects the fact that this code
> represents exactly what I want the computer to do.
> 
> And a working variant of this would be like:
> 
> ===========
> Log = ""
> found = False Error = False S = "lorem ipsum"
> 
> if not found:
>     for s in S:
>         if s == "i" :
>             message = "found on stage 1"
>             found = True
> 
> if not found:
>     S = S + " hello world" for s in S:
>         if s == "d" :
>             message = "found on stage 2"
>             found = True
> 
> if not found:
>     Error = True print "Error : not found"
>     print "S = ", S
> 
> 
> if not Error:
>     print message Log = Log + message
> 
> print "exiting"
> ==============
> 
> This is working code, but I would say there is a lot extra indentation,
> and if I don't care about application scalability, those are just adding
> noise and I it needs some boolean flags.
> 
> I am not sure what examples to add here ...
> it seems to me that e.g. if I would use Python for modelling "pipeline"
> algorithms this could be helpful also.
> 
> Now if I count in the nested loops breaking problematic,
> seems that there is at least a prerequisite for existence of "goto". Am
> I right?
> 
> 
> Mikhail

I expect you could simulate most of these with a custom exception
for example break from nested loop:

class GoTo(Exception):
    pass

try:
    for i in range(100):
        print i
        for j in range (50):
            print j
            if i*j>60:
                raise GoTo
except GoTo:
    print "Exit Early"
print "end of loop"


 



-- 
/*
 * Buddy system. Hairy. You really aren't expected to understand this
 *
 */
        -- From /usr/src/linux/mm/page_alloc.cA
-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to