Op 7/08/20 om 18:45 schreef Chris Angelico: > On Sat, Aug 8, 2020 at 2:21 AM <2qdxy4rzwzuui...@potatochowder.com> wrote: >> >> On 2020-08-07 at 17:55:45 +0200, >> Marco Sulla <marco.sulla.pyt...@gmail.com> wrote: >>> @Chris: note that "real" recursion in Python is not possible, since >>> there's no support for tail recursion. Maybe something similar can be >>> done using async functions. >> >> Python has real recursion. Whether or not there's tail recursion on the >> inside is an implementation detail. > > More specifically: Python has real recursion. Whether or not tail > recursion is optimized away is an implementation detail. > > Tail call optimization (there's no reason to restrict it to recursion > alone) is something a Python implementation could choose to do, but > the trouble is that full optimization tends to destroy traceback > information, so it's often not worth doing.
I don't understand this argument. The trace back information that is destroyed with this optimization, is information that isn't available anyway if you write the code in an iterative fashion. So people are advised to rewrite tail recursive code in an iterative fashion, which results in less trace back information and the reason for not doing tail call optimization is, that it destroy the trace back information they don't have anyway by transforming the code to an iterative version. And the cases where > partial optimization is of value just aren't compelling enough for > anyone to implement it into CPython, nor any other major > implementation (to my knowledge). The biggest uses for TCO tend to be > the situations where recursion is the wrong solution to the problem. I think writing code that is at heart a state machine would be a lot more easy if python would have TCO. Then each state could be implemented by a function and transitioning from one state to the next would be just calling the next function. Sure you can resolve this by writing your state function trampoline style but it forces you into writing some boiler plate that otherwise wouldn't be necessary. -- Antoon Pardon. -- https://mail.python.org/mailman/listinfo/python-list