TCO in Python with exceptions
I was wondering if it would be possible to implement infinite recursion (TCO) in Python by using exceptions to discard the intermediary stack frames, and IT IS POSSIBLE! Here is how, more explanation after the code class Unwind ( Exception ): def init ( self , * args , ** kwargs ): self . args = args self . kwargs = kwargs def tco ( f ): unwind = False def _inner ( * args , ** kwargs ): nonlocal unwind if unwind : raise Unwind ( * args , ** kwargs ) unwind = True while
评论
?
参与讨论