Terry Reedy <tjre...@udel.edu> writes: > Part of the reason that Python does not do tail call optimization is > that turning tail recursion into while iteration is almost trivial, > once you know the secret of the two easy steps. Here it is. > > Assume that you have already done the work of turning a body recursive > ('not tail recursive') form like > > def fact(n): return 1 if n <= 1 else n * fact(n-1) > > into a tail recursion like [...]
How do know that either "<=" or "*" didn't rebind the name "fact" to something else? I think that's the main reason why python cannot apply any procedural optimization (even things like inlining are impossible, or possible only under very conservative assumption, that make it worthless). -- Alain. P/S: don't take me wrong; your explanation is excellent (and very useful to python programmers). What I say is that it relies on assumptions that do not apply to python. -- https://mail.python.org/mailman/listinfo/python-list