Why no tailcall-optimization?

Rhamphoryncus rhamph at gmail.com
Tue Sep 23 23:00:25 CEST 2008


On Sep 22, 7:13 pm, process <circularf... at gmail.com> wrote:
> Why doesn't Python optimize tailcalls? Are there plans for it?
>
> I know GvR dislikes some of the functional additions like reduce and
> Python is supposedly about "one preferrable way of doing things" but
> not being able to use recursion properly is just a big pain in the
> a**.

Eliminating tail calls affects the semantics of your program (by
changing memory complexity).  As an optimization, it's a side-effect
of the implementation, which is a particularly nasty kind of side-
effect.

Even if guaranteed it's fairly subtle.  "return x()" would be
optimized while "return x() + 1" would not.  Or maybe you're calling a
function that uses a wrapper and the wrapper isn't tweaked right to be
optimized.

Recursion in general can be very useful, but not tail-recursion.  The
only time it's not trivial to convert tail-recursion into a loop is
when it involves multiple functions, but even that can be done if you
know how.



More information about the Python-list mailing list