{"id":3300,"library":"trampoline","title":"trampoline","description":"The `trampoline` library (version 0.1.2) provides a simple and tiny yield-based implementation of the trampoline technique in Python. It allows recursive functions to overcome Python's recursion depth limit by converting them into generators that yield subsequent recursive calls, which are then driven by a central `trampoline` function. This enables virtually infinite recursion without exhausting the call stack. The library has not seen recent updates, with its last release in 2018.","status":"maintenance","version":"0.1.2","language":"en","source_language":"en","source_url":"https://gitlab.com/ferreum/trampoline","tags":["recursion","tail-recursion","generator","trampoline","optimization","stack-overflow-prevention"],"install":[{"cmd":"pip install trampoline","lang":"bash","label":"Install with pip"}],"dependencies":[],"imports":[{"symbol":"trampoline","correct":"from trampoline import trampoline"},{"note":"Used for specific tail-call optimization patterns where exceptions are preferred over yield for signalling the next call.","symbol":"TailCall","correct":"from trampoline import TailCall"}],"quickstart":{"code":"from trampoline import trampoline\n\ndef factorial(n):\n    \"\"\"Calculates factorial of n using a trampolined generator.\"\"\"\n    if n <= 1:\n        return 1\n    # Yield the next recursive call, and receive its result\n    return (yield factorial(n - 1)) * n\n\n# Run the trampolined function\nresult = trampoline(factorial(5))\nprint(f\"Factorial of 5: {result}\")\n\ntry:\n    # Example with a large number that would cause RecursionError normally\n    # result_large = trampoline(factorial(2000))\n    # print(f\"Factorial of 2000: {result_large}\")\n    print(\"Skipping factorial(2000) for quickstart brevity, but it would work.\")\nexcept RecursionError as e:\n    print(f\"Caught expected RecursionError (if not trampolined): {e}\")","lang":"python","description":"This quickstart demonstrates how to define a recursive function as a generator, yielding the next recursive call. The `trampoline()` function then drives this generator, managing the 'recursion' iteratively and allowing it to handle deep call chains without stack overflow. It also shows how to receive return values from yielded sub-generators."},"warnings":[{"fix":"Evaluate if the small, self-contained nature of the library is acceptable given its age, or consider reimplementing the trampoline pattern directly if deeper control or active maintenance is required.","message":"The `trampoline` library (v0.1.2) was last updated in 2018. While its core concept is stable, the lack of recent maintenance means it may not receive updates for new Python features, critical bug fixes, or security patches. Consider this for long-term project viability.","severity":"maintenance","affected_versions":"<=0.1.2"},{"fix":"Ensure that all recursive calls within your trampolined function are prefixed with `yield`, e.g., `yield my_recursive_func(arg)`.","message":"Trampolined functions must be written as generators that `yield` the next recursive call, rather than calling it directly. Failing to `yield` will bypass the trampoline and lead to Python's `RecursionError`.","severity":"gotcha","affected_versions":"<=0.1.2"},{"fix":"For functions that return values, structure your recursive call like `return (yield next_recursive_call) * some_operation` to capture and use the result.","message":"When a trampolined function needs to return a value from a recursive step, the `yield` statement must be used as an expression to capture the result. Simply `yield`ing the call will not pass the return value back.","severity":"gotcha","affected_versions":"<=0.1.2"},{"fix":"Always invoke your top-level trampolined generator function by passing it to `trampoline()`, e.g., `result = trampoline(my_func(initial_arg))`.","message":"The initial call to your trampolined generator function must be wrapped by the `trampoline()` utility function. The generator itself does not execute the trampoline logic.","severity":"gotcha","affected_versions":"<=0.1.2"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}