Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Explain to me what the big deal with tail call optimization is and why Python needs it

So apparently, there's been a big brouhaha over whether or not Python needs tail call optimization. This came to a head when someone shipped Guido a copy of SICP because he didn't "get it." I'm in the same boat as Guido. I understand the concept of tail call optimization. I just can't think of any reason why Python really needs it.

To make this easier for me to understand, could someone give me a snippet of code that would be greatly simplified using TCO?

like image 538
Jason Baker Avatar asked May 20 '09 21:05

Jason Baker


People also ask

Does Python have tail-call optimization?

Tail-call optimization is not supported in Python, but we can apply it by changing the code inside the function, however, we prefer to do it automatically using a decorator and without changing the function's code.

What is tail-call optimization What is the benefit of using it?

Tail-call optimization is where you are able to avoid allocating a new stack frame for a function because the calling function will simply return the value that it gets from the called function.

Why Python doesn't have a tail recursion optimization implement it?

There is no built-in tail recursion optimization in Python. However, we can "rebuild" the function through the Abstract Syntax Tree( AST), eliminating the recursion there and replacing it with a loop.

Why tail recursion is important?

This is tail-recursive because the recursive call's return value is returned directly. Tail-recursive functions are important because they can be optimized into loop form: Rather than make a whole new function call, we can simply alter the parameters in memory and jump back to the top of the function.


2 Answers

Personally, I put great value on tail call optimization; but mainly because it makes recursion as efficient as iteration (or makes iteration a subset of recursion). In minimalistic languages you get huge expressive power without sacrificing performance.

In a 'practical' language (like Python), OTOH, you usually have a lot of other constructions for almost every situation imaginable, so it's less critical. It is always a good thing to have, to allow for unforeseen situations, of course.

Personally, I put great value on tail call optimization; but mainly because it makes recursion as efficient as iteration (or makes iteration a subset of recursion). In minimalistic languages you get huge expressive power without sacrificing performance.

In a 'practical' language (like Python), OTOH, you usually have a lot of other constructions for almost every situation imaginable, so it's less critical. It is always a good thing to have, to allow for unforeseen situations, of course.

like image 102
Javier Avatar answered Oct 17 '22 06:10

Javier


If you intensely want to use recursion for things that might alternatively be expressed as loops, then "tail call optimization" is really a must. However, Guido, Python's Benevolent Dictator For Life (BDFL), strongly believes in loops being expressed as loops -- so he's not going to special-case tail calls (sacrificing stack-trace dumps and debugging regularity).

like image 20
Alex Martelli Avatar answered Oct 17 '22 06:10

Alex Martelli