Possibly Pythonic Tail Call Optimization (TCO/TRE)

Ian Kelly ian.g.kelly at gmail.com
Tue Jul 14 09:15:19 EDT 2015


On Tue, Jul 14, 2015 at 2:33 AM, Marko Rauhamaa <marko at pacujo.net> wrote:
> Ian Kelly <ian.g.kelly at gmail.com>:
>
>> On Mon, Jul 13, 2015 at 11:57 PM, Marko Rauhamaa <marko at pacujo.net> wrote:
>>> How about "return"?
>>
>> I think you miss my point entirely. "return" doesn't mean tail-call
>> optimize;
>
> Why not?
>
>> it just means to return the result.
>
> Same difference.
>
>> This is what led to the confusion responsible for the bug that Chris
>> pointed out in the first place. With a keyword that explicitly means
>> "perform tail-call optimization *and* return",
>
> That could well be the explicit definition of the "return" statement in
> Python without changing the behavior of any working Python program
> today.

To the compiler. It won't be viewed that way be the programmer,
however. So they'll forget that the "return" is necessary to the
optimization and just write quicksort() instead of return quicksort().

>> the association of the keyword with the optimization is much clearer,
>> and the programmer is much less likely to mistakenly omit it.
>
> The programmer shouldn't be controlling tail call optimizations.

The programmer controls it regardless.

    return foo()  # TCO

    result = foo()  # No TCO
    return result  # No TCO



More information about the Python-list mailing list