skillfed

trampoline

Simple and tiny yield-based trampoline implementation.

trampoline v0.1.2 1.8M downloads/30d#3,550 on PyPI
Permissive license MIT Abandoned released

What it is and what it does

Trampoline is a minimal yield-based implementation that allows recursive functions to sidestep Python's recursion depth limit by converting them into generators. Instead of calling functions recursively, trampolined functions yield the generator of the next call they want to make; the trampoline function then executes these generators iteratively, effectively simulating unlimited recursion without consuming stack frames.

To use it, you convert a recursive function into a generator by adding yield statements before recursive calls, then wrap the initial call with trampoline(). The package handles return values through yield expressions, preserves exception tracebacks, and supports tail calls via a TailCall exception. It has no external dependencies and consists of roughly 30 lines of core logic, making it lightweight and easy to understand.

Use it for:

  • Computing factorials or other recursive mathematical functions with large inputs that would normally hit the recursion limit.
  • Traversing deeply nested tree or graph structures without stack overflow.
  • Implementing tail-recursive algorithms that benefit from constant-memory execution via TailCall.
  • Writing recursive methods in classes where subclasses need to extend or override trampolined behavior.
  • Processing recursive data structures like nested lists or custom node hierarchies in a single call.

Worth the install?

AI-flagged interpretation of the facts on this page — verify before relying

Enables recursive functions to bypass Python's recursion depth limit by converting them into yield-based generators that the trampoline function executes iteratively.

No. The package is abandoned (last release 2018-08-18) and only declares support for Python 3.5, 3.6, 3.7, making it a poor fit for modern Python environments. While the concept is sound and the code is simple, the lack of maintenance means no compatibility fixes for newer Python versions, no bug fixes, and no assurance it will work with current tooling. For new projects, consider implementing a similar pattern yourself; for legacy code already using it, proceed with caution and test thoroughly on your target Python version.

Install

trampoline on PyPI

pip

pip install trampoline

uv

uv add trampoline

poetry

poetry add trampoline

Installing trampoline

Before you install

Installation is frictionless—a pure Python wheel with no runtime dependencies. However, the package is abandoned; its latest release was 2018-08-18 with no updates since, so it will not receive bug fixes or compatibility patches for newer Python versions.

License in practice

MIT license (permissive) places no restrictions on use, modification, or distribution, making it safe to incorporate into commercial or open-source projects.

Quickstart

from trampoline import trampoline

def factorial(n):
    if n <= 1:
        return 1
    value = yield factorial(n - 1)
    return value * n

result = trampoline(factorial(10))
print(result)  # 3628800

Functions must be rewritten as generators using yield at recursive call sites; existing recursive code cannot be used directly without modification.

Verify before relying

  • Whether the package works reliably with Python versions beyond 3.7 (classifiers list only 3.5, 3.6, 3.7).
  • Performance characteristics when recursing deeply, given the description mentions memory consumption as a caveat.

Package facts

License MIT (permissive)
Python support not specified
Install friction low — pure-Python wheel
Runtime dependencies none
Maintenance abandoned — 2,918 days since the last release
First released
Downloads 1,796,619/month — #3,550 on PyPI (30-day window, as of 2026-08-14)
Known vulnerabilities none known (OSV.dev, checked 2026-08-14)

Evidence: trampoline-0.1.2-py3-none-any.whl

Keywords: trampoline, recursion, tail, call

Development Status :: 3 - AlphaIntended Audience :: DevelopersLicense :: OSI Approved :: MIT LicenseNatural Language :: EnglishOperating System :: OS IndependentProgramming Language :: Python :: 3.5Programming Language :: Python :: 3.6Programming Language :: Python :: 3.7

Tags

recursion depth limittail call optimizationgenerator-based recursionstack overflow preventiondeep recursionyield-based trampolineunlimited recursion
recursion-optimizationgenerator-pattern

More Software Development packages