lambda expressions

James_Althoff at i2.com James_Althoff at i2.com
Tue Feb 5 14:29:17 EST 2002


[Jason Orendorff]
>I should have said "Lack of tail call optimization could
>be a problem."
>
>For spectators:  the reason tail recursion is important
>at all is, any tail call can be compiled into (essentially)
>a goto.  This allows functions like WHILE() to loop indefinitely
>without eating up memory.
>
>In Python, tail calls are not optimized.  So in Python this
>uses O(N) memory, where N = the number of times the loop
>executes. Eventually you get "RuntimeError: maximum recursion
>depth exceeded".

As Jason points out, any tail call -- not just recursive calls --
potentially can be optimized.  A downside (the upside is mentioned above)
-- in addition to some added complexity of implementation -- is that you
pay the piper sometimes when debugging since you lose all intermediate
context (by overwriting the stack frame early -- hence the savings in stack
memory).

Jim





More information about the Python-list mailing list