"Goto" statement in Python

Marko Rauhamaa marko at pacujo.net
Thu Apr 13 15:05:56 EDT 2017


Rob Gaddi <rgaddi at highlandtechnology.invalid>:

> On 04/13/2017 08:26 AM, Marko Rauhamaa wrote:
>> I have occasionally felt the urge to try "goto" in my C code, but having
>> written it, I have taken it out. It just doesn't make the code look more
>> elegant or robust. Unlike "break" or "return," "goto" makes me uneasy
>> about variable scope and lifetime.
> [...]
> You see it all the time in kernel code or when doing I/O.  A pretty
> common pattern is:
>
>   int return_val = 1;
>   if (init_thing(x)) goto bk1;
>   if (init_thing(y)) goto bk2;
>   if (init_thing(z)) goto bk3;
>
>   do_things_with(x, y, z);
>   return_val = 0;
>
>   bk3:  cleanup_thing(z);
>   bk2:  cleanup_thing(y);
>   bk1:  cleanup_thing(x);
>   return return_val;

I know, but I still don't like it.


Marko


More information about the Python-list mailing list