[Python-ideas] A Continuations Compromise in Python

Terry Reedy tjreedy at udel.edu
Mon May 4 00:20:50 CEST 2009


Adam Olsen wrote:
> On Sun, May 3, 2009 at 1:02 AM, Steven D'Aprano <steve at pearwood.info> wrote:

>> Up until a month or so ago, I'd never heard of the term "trampoline"
>> (apart from the thing you jump up and down on), and I still don't know
>> what it means. Checking Wikipedia, I see that in computing, trampoline
>> has *eleven* definitions. Which one do you mean?

I was pretty much in the same position also.

>     * Used in some LISP implementations, a trampoline is a loop that
> iteratively invokes thunk-returning functions. A single trampoline is
> sufficient to express all control transfers of a program; 

Aha.  This is a restatement of the decades-old fact that a while loop 
and goto are sufficient to implement any flow chart.

> For example, something like this (but obviously with arguments

Or a global state structure.

> and the ability to finish looping):

Not sure what you mean, unless a way to exit the mainloop.

> def a():
>     return b
> 
> def b():
>     return a
> 
> def trampoline(func):
>     while True:
>         func = func()

So the modern version is functions returning the next function to call 
instead of code-chunks setting the next label to jump to.  Of course, 
that means that one can match the worst spaghetti-code mess with this 
mechanism also ;-).

Thanks for a clear explanation.

tjr




More information about the Python-ideas mailing list