Why no tailcall-optimization?

Jean-Paul Calderone exarkun at divmod.com
Tue Sep 23 15:55:43 CEST 2008


On Tue, 23 Sep 2008 06:41:33 -0700 (PDT), sturlamolden <sturlamolden at yahoo.no> wrote:
>On Sep 23, 3:13 am, process <circularf... at gmail.com> wrote:
>
>> Why doesn't Python optimize tailcalls? Are there plans for it?
>
>Because Python is a dynamic language. While a function is executing,
>its name may be bound to another object. It may happen as a side
>effect of what the function is doing, or even from another thread. The
>compiler has no way of knowing that.

That's not the reason.  It's not particularly hard to implement TCE for
Python.  Instead, Guido has repeatedly given the reason as his feeling
that recursion is not the most natural way to express most algorithms
in Python.  Adding TCE to the runtime would encourage people to write
(or continue to write) highly recursive algorithms, which would go
against what Guido thinks is "Pythonic".  Here's a quote from him on
the topic:

> But I have a problem with tail recursion.  It's generally requested by
> new converts from the Scheme/Lisp or functional programming world, and
> it usually means they haven't figured out yet how to write code
> without using recursion for everything yet.  IOW I'm doubtful on how
> much of a difference it would make for real Python programs (which,
> simplifying a bit, tend to use loops instead of recursion).

Jean-Paul



More information about the Python-list mailing list