This repository has been archived by the owner. It is now read-only.
Tail call optimization for Python.
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.


Do not use this.

A direct tail call optimizing decorator for Python.


from tcopy import tco

def fib(n, x=0, y=1):
    if n == 0:
        return x
    return fib(n - 1, y, x + y)

The tco decorator will rewrite fib into the following at definition time:

def fib(n, x=0, y=1):
    while 1:
        if n == 0:
            return x
        n, x, y = n - 1, y, x + y


tco uses inspect.getsource to grab a function's source code from disk. Because of this, the decorator does not work in the Python REPL.