[Python-ideas] Tail Call Optimization (was Re: Tail recursion elimination)

Terry Reedy tjreedy at udel.edu
Sun Jan 19 13:12:16 CET 2014


TCO (Tail Call Optimization) means that when TCO is in effect and a tail 
call "return f(<args>)" is executed, the current execution context 
(stack frame) is used for the call instead of allocating a new one. What 
is 'optimized' is space usage. The effect on time is not clear.

On 1/18/2014 7:45 PM, Steven D'Aprano wrote:

> What makes you say that it is "non-pythonic"? You seem to be assuming
> that *by definition* anything written recursively is non-pythonic. I do
> not subscribe to that view.

Neither do I.

> In fact, in some cases, I *would* willingly give up *non-useful*
> tracebacks for the ability to write more idiomatic code.
> The point is that tracebacks are not sacrosanct, and, yes, I would like
> the choice between writing idiomatic recursive code and more extensive
> tracebacks. Trading off speed for convenience is perfectly Pythonic --
> that's why we have the ability to write C extensions, is it not?

Are you willing to do any of the work needed to make the option 
available, starting with a specification? If so, I have some ideas.

> Having to fork the entire compiler just to write a few functions in
> their most idiomatic, natural (recursive) form seems a bit extreme,
> wouldn't you say?

A 'fork' could consist of a relatively small patch that could be 
uploaded to, for instance, PyPI. I would not be surprised if 100-200 
lines might be enough.

-- 
Terry Jan Reedy



More information about the Python-ideas mailing list