Tail recursion in Python

There’s an interesting post on tail recursion in Python from Chris Penner (actually an old post, but I’ve only just seen it).

Chris comes up with a way of allowing functions in Python to be tail-recursive, that is to be able to call themselves without exhausting the limited stack space in Python.

In other languages tail-recursion is used as a substitute for iteration, in order to express iterative algorithms without having to have mutable local state. That’s not really a good idea in Python. But there may be a role for tail recursion in expressing fundamentally recursive algorithms which would otherwise get limited by lack of stack space.

Leave a Reply

Your email address will not be published. Required fields are marked *