[Python-Dev] Proper tail recursion

Michael Chermside mcherm at mcherm.com
Thu Jul 15 14:24:54 CEST 2004

Christopher King writes:
> To tell the truth, I don't really have any immediate use for this
> functionality, either, but since it turned out to be so
> easy to implement,
> I ask, "why not?" ;)

Martin replies:
> The traditional answer to that question is "because Jython cannot
> support it".

No... that answer applies to *language features*, but not *implementation
details*. The recursion limit (the value of it anyhow) is an
implementation detail.

Think of it this way: if Iron Python, Jython, or PyPy were to implement
a clever trick that made loops run 10x faster, that would be swell,
even if the same trick couldn't be done in CPython. If CPython can find
a way to run recursion far more efficiently, then that's swell too. Of
course, the actual results from running the program must not differ,
but that doesn't seem too difficult to achieve.

Guido writes:
> I'm not interested in adding this to the official Python release.
> One reason is that if an exception happens in such a tail-recursive
> call, the stack trace will be confusing.

This doesn't bother me as much as it apparently bothers you. But for
that matter, we hardly care about performance if we're going to be
generating a stack trace, so we could probably construct the stack
trace after-the-fact if needed.

Guido continues:

More information about the Python-Dev mailing list