GOTO w/ Python?
Jerzy Karczmarczuk
karczma at info.unicaen.fr
Thu Jun 20 09:37:31 EDT 2002
John Roth after Michael Hudson after Max M.:
> > Goto's are simple ;-)
> > def goto(lable):
> > lable()
> > def start():
> >
> > print 'starting'
> > goto(lable2)
> >
> > def lable1():
> >
> > print 'at lable 1'
> > goto(stop)
etc.
> Those aren't goto's in the classical sense. For a real goto,
> you need to think assembler: if it's a statement anywhere,
> you can branch to it, and who cares about the execution
> history! Having the compiler manage your flow control is
> for sissys!
Gentlemen, what are you talking about?
(I confess I didn't follow this thread, so forgive me if I
try to open a non-existent door)
Before, John Roth suggested using structured exceptions as GOTOs,
and Michael Hudson wrote something like that:
> There are also technical difficulties implementing it (the
> blockstack), or I'd have probably done goto functionality in
> bytecodehacks :)
Well...
1. The "solution" of Max M. is erroneous for two reasons.
A. A procedure call stacks the return address, so a few hundreds
such gotos, and the system blows up.
Unfortunately Python does not optimize tail calls, I WONDER WHY;
for a member of the Functional Programming Church this is
a mortal sin, and I am serious.
B. The code chunks linked in such a way provide different variable
scopes, so there is little sense in calling it a "branching".
2. "Structured exception" is something quite heavy, it may be heavier
than the setjump/longjump contraption, and may also be useless to
those who simply would love to do some Python programming à la
Fortran.
3. No, in order to speak about genuine gotos *you don't need to think
assembler*.
What you need is to understand correctly the notion of continuation
(something quite standard - again - in the Functional Programming
world). This is a high-level model of "flat" branching, but may be
used to more exquisite constructions, such as backtracking.
I don't believe that there are any technical difficulties in the
implementation of gotos at the low level. At the high level this
is then a question of the language structure, as seen by Van Rossum,
the lack of gotos was a conscious decision.
Jerzy Karczmarczuk
Caen, France
More information about the Python-list
mailing list