Discussion:
[Numpy-discussion] Python needs goto
Charles R Harris
2015-09-24 05:24:21 UTC
Permalink
At last, goto for python <https://github.com/snoack/python-goto>!

Usage:

from goto import with_goto

@with_goto
def range(start, stop):
i = start
result = []

label .begin
if i == stop:
goto .end

result.append(i)
i += 1
goto .begin

label .end
return result


HT: LWN

Chuck
Christophe Bal
2015-09-24 11:25:16 UTC
Permalink
Hello.

Can you give an example where GOTO is useful ?
Post by Charles R Harris
At last, goto for python <https://github.com/snoack/python-goto>!
from goto import with_goto
@with_goto
i = start
result = []
label .begin
goto .end
result.append(i)
i += 1
goto .begin
label .end
return result
HT: LWN
Chuck
_______________________________________________
NumPy-Discussion mailing list
https://mail.scipy.org/mailman/listinfo/numpy-discussion
R Schumacher
2015-09-24 14:41:57 UTC
Permalink
An older non-decorator implementation, with examples (slower)
http://entrian.com/goto/
Post by Christophe Bal
Hello.
Can you give an example where GOTO is useful ?
Le 24 sept. 2015 07:24, "Charles R Harris"
At last, <https://github.com/snoack/python-goto>goto for python!
from goto import with_goto
@with_goto
i = start
result = []
label .begin
goto .end
result.append(i)
i += 1
goto .begin
label .end
return result
HT: LWN
Chuck
_______________________________________________
NumPy-Discussion mailing list
https://mail.scipy.org/mailman/listinfo/numpy-discussion
_______________________________________________
NumPy-Discussion mailing list
https://mail.scipy.org/mailman/listinfo/numpy-discussion
Alexander Eberspächer
2015-09-24 17:54:19 UTC
Permalink
Post by Christophe Bal
Can you give an example where GOTO is useful ?
I think those pieces of code are best understood with some humour..

However, basically I can think two main causes for using goto:

1. Stop whatever your code is doing and jump towards the end of the
program. However, this is mainly something useful for languages without
exception handling and garbage collection.

2. Get out of something deeply nested. Also, this probably isn't very
useful in Python as there's exception handling.

Best

Alex
Yarko Tymciurak
2015-09-24 18:13:10 UTC
Permalink
On Thu, Sep 24, 2015 at 12:54 PM, Alexander EberspÀcher <
Post by Alexander Eberspächer
Post by Christophe Bal
Can you give an example where GOTO is useful ?
I think those pieces of code are best understood with some humour..
1. Stop whatever your code is doing and jump towards the end of the
program. However, this is mainly something useful for languages without
exception handling and garbage collection.
2. Get out of something deeply nested. Also, this probably isn't very
useful in Python as there's exception handling.
I think there are more valid uses - I've read that "goto" basically is what
a state machine does.
Have a read of the brief implementation notes for "goto" in golang, for
example. Goto may not be unreasonable to use, just most people would
abuse. Sort of like "everyone shouldn't write assembly, but if you
understand the machine, you can make good things happen". Without
compiler/interpreter checks, more responsibility rests on the coder to keep
out of trouble.
Post by Alexander Eberspächer
Best
Alex
_______________________________________________
NumPy-Discussion mailing list
https://mail.scipy.org/mailman/listinfo/numpy-discussion
Charles R Harris
2015-09-24 18:41:57 UTC
Permalink
Post by Yarko Tymciurak
On Thu, Sep 24, 2015 at 12:54 PM, Alexander EberspÀcher <
Post by Alexander Eberspächer
Post by Christophe Bal
Can you give an example where GOTO is useful ?
I think those pieces of code are best understood with some humour..
1. Stop whatever your code is doing and jump towards the end of the
program. However, this is mainly something useful for languages without
exception handling and garbage collection.
2. Get out of something deeply nested. Also, this probably isn't very
useful in Python as there's exception handling.
I think there are more valid uses - I've read that "goto" basically is
what a state machine does.
Have a read of the brief implementation notes for "goto" in golang, for
example. Goto may not be unreasonable to use, just most people would
abuse. Sort of like "everyone shouldn't write assembly, but if you
understand the machine, you can make good things happen". Without
compiler/interpreter checks, more responsibility rests on the coder to keep
out of trouble.
I would agree about state machines. When implemented using the standard
control flow constructs they always look a bit artificial.

Chuck
Benjamin Root
2015-09-24 18:50:10 UTC
Permalink
Most of the time when I wanted to use goto in my early days, I found that
breaks and continues were better and easier to understand. I will admit
that there are occasional nested if/elif/else code that get messy without a
goto. But which smells worse? A "goto" package or a complex if/elif/else?

Ben Root
Post by Charles R Harris
Post by Yarko Tymciurak
On Thu, Sep 24, 2015 at 12:54 PM, Alexander EberspÀcher <
Post by Alexander Eberspächer
Post by Christophe Bal
Can you give an example where GOTO is useful ?
I think those pieces of code are best understood with some humour..
1. Stop whatever your code is doing and jump towards the end of the
program. However, this is mainly something useful for languages without
exception handling and garbage collection.
2. Get out of something deeply nested. Also, this probably isn't very
useful in Python as there's exception handling.
I think there are more valid uses - I've read that "goto" basically is
what a state machine does.
Have a read of the brief implementation notes for "goto" in golang, for
example. Goto may not be unreasonable to use, just most people would
abuse. Sort of like "everyone shouldn't write assembly, but if you
understand the machine, you can make good things happen". Without
compiler/interpreter checks, more responsibility rests on the coder to keep
out of trouble.
I would agree about state machines. When implemented using the standard
control flow constructs they always look a bit artificial.
Chuck
_______________________________________________
NumPy-Discussion mailing list
https://mail.scipy.org/mailman/listinfo/numpy-discussion
Christophe Bal
2015-09-24 18:56:00 UTC
Permalink
To be honest, I am not very conviced. Does someone can give a pseudo
example ?
Post by Benjamin Root
Most of the time when I wanted to use goto in my early days, I found that
breaks and continues were better and easier to understand. I will admit
that there are occasional nested if/elif/else code that get messy without a
goto. But which smells worse? A "goto" package or a complex if/elif/else?
Ben Root
On Thu, Sep 24, 2015 at 2:41 PM, Charles R Harris <
Post by Charles R Harris
Post by Yarko Tymciurak
On Thu, Sep 24, 2015 at 12:54 PM, Alexander EberspÀcher <
Post by Alexander Eberspächer
Post by Christophe Bal
Can you give an example where GOTO is useful ?
I think those pieces of code are best understood with some humour..
1. Stop whatever your code is doing and jump towards the end of the
program. However, this is mainly something useful for languages without
exception handling and garbage collection.
2. Get out of something deeply nested. Also, this probably isn't very
useful in Python as there's exception handling.
I think there are more valid uses - I've read that "goto" basically is
what a state machine does.
Have a read of the brief implementation notes for "goto" in golang, for
example. Goto may not be unreasonable to use, just most people would
abuse. Sort of like "everyone shouldn't write assembly, but if you
understand the machine, you can make good things happen". Without
compiler/interpreter checks, more responsibility rests on the coder to keep
out of trouble.
I would agree about state machines. When implemented using the standard
control flow constructs they always look a bit artificial.
Chuck
_______________________________________________
NumPy-Discussion mailing list
https://mail.scipy.org/mailman/listinfo/numpy-discussion
_______________________________________________
NumPy-Discussion mailing list
https://mail.scipy.org/mailman/listinfo/numpy-discussion
Christophe Bal
2015-09-24 18:57:13 UTC
Permalink
By pseudo example, I mean pseudo code.
Post by Christophe Bal
To be honest, I am not very conviced. Does someone can give a pseudo
example ?
Post by Benjamin Root
Most of the time when I wanted to use goto in my early days, I found that
breaks and continues were better and easier to understand. I will admit
that there are occasional nested if/elif/else code that get messy without a
goto. But which smells worse? A "goto" package or a complex if/elif/else?
Ben Root
On Thu, Sep 24, 2015 at 2:41 PM, Charles R Harris <
Post by Charles R Harris
Post by Yarko Tymciurak
On Thu, Sep 24, 2015 at 12:54 PM, Alexander EberspÀcher <
Post by Alexander Eberspächer
Post by Christophe Bal
Can you give an example where GOTO is useful ?
I think those pieces of code are best understood with some humour..
1. Stop whatever your code is doing and jump towards the end of the
program. However, this is mainly something useful for languages without
exception handling and garbage collection.
2. Get out of something deeply nested. Also, this probably isn't very
useful in Python as there's exception handling.
I think there are more valid uses - I've read that "goto" basically is
what a state machine does.
Have a read of the brief implementation notes for "goto" in golang, for
example. Goto may not be unreasonable to use, just most people would
abuse. Sort of like "everyone shouldn't write assembly, but if you
understand the machine, you can make good things happen". Without
compiler/interpreter checks, more responsibility rests on the coder to keep
out of trouble.
I would agree about state machines. When implemented using the standard
control flow constructs they always look a bit artificial.
Chuck
_______________________________________________
NumPy-Discussion mailing list
https://mail.scipy.org/mailman/listinfo/numpy-discussion
_______________________________________________
NumPy-Discussion mailing list
https://mail.scipy.org/mailman/listinfo/numpy-discussion
Charles R Harris
2015-09-24 19:12:46 UTC
Permalink
Post by Christophe Bal
By pseudo example, I mean pseudo code.
I find Cleve Moler's old Fortran version of Brent's zero finding algorithm
using gotos clearer than the structured versions you can find in Numerical
Recipes. The operation of the algorithm is easiest to describe as a finite
state machine. As others have said, goto is also useful for languages
without exception handling, C for instance. You will find gotos in the
Linux kernel and in the Numpy core used for that purpose.

However, the post was for laughs, I didn't expect serious discussion. The
current thread is a pleasant surprise.

Chuck
Alexander Eberspächer
2015-09-25 05:53:41 UTC
Permalink
Post by Charles R Harris
I find Cleve Moler's old Fortran version of Brent's zero finding
algorithm using gotos clearer than the structured versions you can find
in Numerical Recipes. The operation of the algorithm is easiest to
describe as a finite state machine.
I need to look into that piece of Fortran code. I've never (knowingly)
implemented a state-machine myself. However I can say that specifically
Fortran has named loops, which probably helps to avoid many uses of goto.
Post by Charles R Harris
However, the post was for laughs, I didn't expect serious discussion.
Well, then let me fill in the missing piece - the obligatory XKCD strip:

https://www.xkcd.com/292/

Alex
Christophe Bal
2015-09-25 11:11:30 UTC
Permalink
Very funny ! 😂
Post by Alexander Eberspächer
Post by Charles R Harris
I find Cleve Moler's old Fortran version of Brent's zero finding
algorithm using gotos clearer than the structured versions you can find
in Numerical Recipes. The operation of the algorithm is easiest to
describe as a finite state machine.
I need to look into that piece of Fortran code. I've never (knowingly)
implemented a state-machine myself. However I can say that specifically
Fortran has named loops, which probably helps to avoid many uses of goto.
Post by Charles R Harris
However, the post was for laughs, I didn't expect serious discussion.
https://www.xkcd.com/292/
Alex
_______________________________________________
NumPy-Discussion mailing list
https://mail.scipy.org/mailman/listinfo/numpy-discussion
William Ray Wing
2015-09-25 15:21:52 UTC
Permalink
Post by Alexander Eberspächer
Post by Charles R Harris
I find Cleve Moler's old Fortran version of Brent's zero finding
algorithm using gotos clearer than the structured versions you can find
in Numerical Recipes. The operation of the algorithm is easiest to
describe as a finite state machine.
I need to look into that piece of Fortran code. I've never (knowingly)
implemented a state-machine myself. However I can say that specifically
Fortran has named loops, which probably helps to avoid many uses of goto.
GOTOs really can be useful. COMPUTED GOTOs, on the other hand, are a debugging nightmare, literally!!!

Bill
Post by Alexander Eberspächer
Post by Charles R Harris
However, the post was for laughs, I didn't expect serious discussion.
https://www.xkcd.com/292/
Alex
_______________________________________________
NumPy-Discussion mailing list
https://mail.scipy.org/mailman/listinfo/numpy-discussion
Anne Archibald
2015-09-25 16:27:50 UTC
Permalink
goto! and comefrom! Together with exceptions, threads, lambda, super,
generators, and coroutines, all we're lacking is
call-with-current-continuation for the full list of impenetrable
control-flow constructs. Oh, and lisp-style resumable exception handling.
(Suggested syntax: drop(exception, value) to return control to where the
exception was raised and make the raise statement return value.)
Post by Charles R Harris
Post by Yarko Tymciurak
I think there are more valid uses - I've read that "goto" basically is
what a state machine does.
Have a read of the brief implementation notes for "goto" in golang, for
example. Goto may not be unreasonable to use, just most people would
abuse. Sort of like "everyone shouldn't write assembly, but if you
understand the machine, you can make good things happen". Without
compiler/interpreter checks, more responsibility rests on the coder to keep
out of trouble.
I would agree about state machines. When implemented using the standard
control flow constructs they always look a bit artificial.
That depends what your "standard" control flow constructs are. Has anyone
tried implementing a state machine using coroutines? They seem like a
rather natural setup: each state is a coroutine that loops, doing the
appropriate actions for the state and then handing control over to the
coroutine for the next state.

Anne
Benjamin Root
2015-09-25 16:40:27 UTC
Permalink
Ow! Ow! Ow! I am just a meteorologist that has an obsession with looking up
unfamiliar technology terms.

I need a Tylenol...
Ben Root
Post by Anne Archibald
goto! and comefrom! Together with exceptions, threads, lambda, super,
generators, and coroutines, all we're lacking is
call-with-current-continuation for the full list of impenetrable
control-flow constructs. Oh, and lisp-style resumable exception handling.
(Suggested syntax: drop(exception, value) to return control to where the
exception was raised and make the raise statement return value.)
On Thu, Sep 24, 2015 at 8:42 PM Charles R Harris <
Post by Charles R Harris
Post by Yarko Tymciurak
I think there are more valid uses - I've read that "goto" basically is
what a state machine does.
Have a read of the brief implementation notes for "goto" in golang, for
example. Goto may not be unreasonable to use, just most people would
abuse. Sort of like "everyone shouldn't write assembly, but if you
understand the machine, you can make good things happen". Without
compiler/interpreter checks, more responsibility rests on the coder to keep
out of trouble.
I would agree about state machines. When implemented using the standard
control flow constructs they always look a bit artificial.
That depends what your "standard" control flow constructs are. Has anyone
tried implementing a state machine using coroutines? They seem like a
rather natural setup: each state is a coroutine that loops, doing the
appropriate actions for the state and then handing control over to the
coroutine for the next state.
Anne
_______________________________________________
NumPy-Discussion mailing list
https://mail.scipy.org/mailman/listinfo/numpy-discussion
Charles R Harris
2015-09-25 18:06:01 UTC
Permalink
Post by Anne Archibald
goto! and comefrom! Together with exceptions, threads, lambda, super,
generators, and coroutines, all we're lacking is
call-with-current-continuation for the full list of impenetrable
control-flow constructs. Oh, and lisp-style resumable exception handling.
(Suggested syntax: drop(exception, value) to return control to where the
exception was raised and make the raise statement return value.)
On Thu, Sep 24, 2015 at 8:42 PM Charles R Harris <
Post by Charles R Harris
Post by Yarko Tymciurak
I think there are more valid uses - I've read that "goto" basically is
what a state machine does.
Have a read of the brief implementation notes for "goto" in golang, for
example. Goto may not be unreasonable to use, just most people would
abuse. Sort of like "everyone shouldn't write assembly, but if you
understand the machine, you can make good things happen". Without
compiler/interpreter checks, more responsibility rests on the coder to keep
out of trouble.
I would agree about state machines. When implemented using the standard
control flow constructs they always look a bit artificial.
That depends what your "standard" control flow constructs are. Has anyone
tried implementing a state machine using coroutines? They seem like a
rather natural setup: each state is a coroutine that loops, doing the
appropriate actions for the state and then handing control over to the
coroutine for the next state.
Might well do. TAOCP has an example elevator, passenger simulation that,
IIRC, used coroutines. I think I may have even once used a language that
had them (Algol 68?). It will be interesting to see what their inclusion in
Python 3.5 leads to.

Chuck
Nathaniel Smith
2015-09-25 21:02:41 UTC
Permalink
Post by Charles R Harris
Post by Anne Archibald
goto! and comefrom! Together with exceptions, threads, lambda, super,
generators, and coroutines, all we're lacking is
call-with-current-continuation for the full list of impenetrable
control-flow constructs. Oh, and lisp-style resumable exception handling.
(Suggested syntax: drop(exception, value) to return control to where the
exception was raised and make the raise statement return value.)
Post by Charles R Harris
Post by Anne Archibald
On Thu, Sep 24, 2015 at 8:42 PM Charles R Harris <
Post by Charles R Harris
Post by Yarko Tymciurak
I think there are more valid uses - I've read that "goto" basically is
what a state machine does.
Post by Charles R Harris
Post by Anne Archibald
Post by Charles R Harris
Post by Yarko Tymciurak
Have a read of the brief implementation notes for "goto" in golang,
for example. Goto may not be unreasonable to use, just most people would
abuse. Sort of like "everyone shouldn't write assembly, but if you
understand the machine, you can make good things happen". Without
compiler/interpreter checks, more responsibility rests on the coder to keep
out of trouble.
Post by Charles R Harris
Post by Anne Archibald
Post by Charles R Harris
I would agree about state machines. When implemented using the standard
control flow constructs they always look a bit artificial.
Post by Charles R Harris
Post by Anne Archibald
That depends what your "standard" control flow constructs are. Has
anyone tried implementing a state machine using coroutines? They seem like
a rather natural setup: each state is a coroutine that loops, doing the
appropriate actions for the state and then handing control over to the
coroutine for the next state.
Post by Charles R Harris
Might well do. TAOCP has an example elevator, passenger simulation that,
IIRC, used coroutines. I think I may have even once used a language that
had them (Algol 68?). It will be interesting to see what their inclusion in
Python 3.5 leads to.

The coroutines in 3.5 are just syntactic sugar around features that were
added in *2*.5 (yield expressions and yield from), so no need to wait :-).
They fall far short of arbitrary continuations, though.

I don't think it's particularly cumbersome to implement a state machine
with one function per state and the return value specifying the new state
-- there's a little more typing to set up the local variables and such each
time, but if you have complex states then this seems like even an advantage
over goto (since with goto you may have to set up local state by hand each
time, basically reimplementing your own scoping mechanism). Of course if
all your states are trivial then this overhead is just annoying.

...I suspect you actually might be able to implement resumable exceptions
in cpython as a (very evil) third party module. I'm going to stop thinking
about that now before I get too curious and do something I regret.

Actually, don't stackless / greenlet give you full continuations?

-n
Ryan May
2015-09-25 21:39:26 UTC
Permalink
Post by Nathaniel Smith
The coroutines in 3.5 are just syntactic sugar around features that were
added in *2*.5 (yield expressions and yield from), so no need to wait :-).
They fall far short of arbitrary continuations, though.
Correction: Python 3.4 gained "yield from". Prior to that, you had a lot of
work to properly delegate from one generator to another.

But yes, async and await are just syntactic sugar (consistent with other
languages) for python 3.4's coroutine functionality.

Ryan
Nathaniel Smith
2015-09-25 22:59:39 UTC
Permalink
Post by Ryan May
Post by Nathaniel Smith
The coroutines in 3.5 are just syntactic sugar around features that were
added in *2*.5 (yield expressions and yield from), so no need to wait :-).
They fall far short of arbitrary continuations, though.
Correction: Python 3.4 gained "yield from". Prior to that, you had a lot of
work to properly delegate from one generator to another.
Ah, right, thanks for the correction. Not sure how much yield from
matters for state machines, but certainly it's great for async IO.

-n
--
Nathaniel J. Smith -- http://vorpus.org
Jason Newton
2015-09-24 18:39:37 UTC
Permalink
In my experience, it's also come up with finite-state-machines where
there's lots of loops. You might consider something like a long-lived
client-loop on some socket, where states like try-connect, connected, and
while-connected-and-everythings-ok exist and each can have it's own never
ending loops and each loop jumps to each others section. It actually is
more straightforward than adding unneeded layers like callbacks which will
force you to design and pass around data, use auxiliary classes/structs,
and it's easier to shoot yourself in the foot with burden and context loss
that way.

-Jason

On Thu, Sep 24, 2015 at 1:54 PM, Alexander EberspÀcher <
Post by Alexander Eberspächer
Post by Christophe Bal
Can you give an example where GOTO is useful ?
I think those pieces of code are best understood with some humour..
1. Stop whatever your code is doing and jump towards the end of the
program. However, this is mainly something useful for languages without
exception handling and garbage collection.
2. Get out of something deeply nested. Also, this probably isn't very
useful in Python as there's exception handling.
Best
Alex
_______________________________________________
NumPy-Discussion mailing list
https://mail.scipy.org/mailman/listinfo/numpy-discussion
Continue reading on narkive:
Loading...