[Python-ideas] A Continuations Compromise in Python

Adam Olsen rhamph at gmail.com
Sun May 3 23:22:55 CEST 2009


On Sun, May 3, 2009 at 2:56 PM, John Graham <john.a.graham at gmail.com> wrote:
> On Sun, May 3, 2009 at 3:54 PM, Adam Olsen <rhamph at gmail.com> wrote:
>> On Sat, May 2, 2009 at 5:55 PM, John Graham <john.a.graham at gmail.com> wrote:
>>> The pattern here, basically, that continue eliminates, is the constant
>>> referral to 'just use a trampoline function'.  To me, language
>>> constructs exist to codify certain patterns, similar to the way list
>>> comprehensions captured a lot of what was previously done in for
>>> loops.
>>
>> No, it's not about codifying.  It's about having a *significantly
>> better* solution by modifying the language than working within the
>> language.  List comprehensions are significantly better than a full
>> for-loop.  Adding a keyword is not significantly better than returning
>> your next function; it's actually worse.
>>
>> It's a non-solution to a non-problem.  If you actually *had* a problem
>> you could do it with trampolines.  They do exactly what's needed, they
>> just don't put a bow on it.  Ho hum.
>
> Could you be more elaborate on what attributes you're looking for that
> make something "significantly better"?

It's fairly subjective, but lists vs genexps make a good example:

data = []
for i in range(50):
    data.append[i**2]
use_result(data)

vs

use_result(i**2 for i in range(50))

Of course a listcomp would be a more direct comparison, but I think
the argument is stronger because it's *not* a direct translation.
You'd have to write a generator function, which is enough clumsier
that nobody would even consider it, before genexps were added.

In contrast, we have the before with trampolines:

def a():
    return (b, arg1, arg2)

and the after:

def a():
    continue b(arg1, arg2)

Sure, it's prettier to look like a function call, but you're
disguising the fact that you're not calling it as a function right
now.  You're also disguising that you're returning from this function.

If explaining the trampoline to others is the issue, why not call it a
"pop and call" operation?  Pop off the old stack frame, then do your
call.  Or call it "return and call", if they don't understand stack
frames.

-- 
Adam Olsen, aka Rhamphoryncus



More information about the Python-ideas mailing list