{"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":"python","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,"search_vec":"'0.1.2':6 '2018':74 'allow':23 'base':14 'call':42,60 'central':49 'convert':34 'depth':31 'driven':46 'enabl':53 'exhaust':58 'function':25,51 'generat':37,79 'implement':15 'infinit':55 'last':71 'librari':4,63 'limit':32 'optim':81 'overcom':27 'overflow':84 'prevent':85 'provid':7 'python':21,28 'recent':67 'recurs':24,30,41,56,75,78 'releas':72 'seen':66 'simpl':9 'stack':61,83 'stack-overflow-prevent':82 'subsequ':40 'tail':77 'tail-recurs':76 'techniqu':19 'tini':11 'trampolin':1,3,18,50,80 'updat':68 'version':5 'virtual':54 'without':57 'yield':13,39 'yield-bas':12","created_at":"2026-04-11T09:29:35.572626+00:00","updated_at":"2026-04-16T23:11:51.530434+00:00","problems":[{"fix":"When using the `@trampoline` decorator, ensure recursive calls are `return func.call(...)`. When using the `trampoline(func)(...)` wrapper, ensure recursive calls are `return (func, args)` to properly hand off control.","cause":"The recursive function, despite being intended for the trampoline technique, is making direct recursive calls instead of yielding subsequent calls using the trampoline-specific syntax.","error":"RecursionError: maximum recursion depth exceeded"},{"fix":"Install the library using pip: `pip install trampoline`.","cause":"The `trampoline` library has not been installed in the Python environment, or the environment where the script is being run does not have it installed.","error":"ModuleNotFoundError: No module named 'trampoline'"},{"fix":"Ensure the recursive function is correctly decorated with `@trampoline`. Verify that all recursive calls within the decorated function use the `return func.call(...)` syntax, not `return func(...)`.","cause":"This error occurs when a function is expected to have the `.call` method added by the `@trampoline` decorator, but the decorator was either not applied or the function was invoked directly without the trampoline mechanism.","error":"AttributeError: 'function' object has no attribute 'call'"},{"fix":"Ensure that the function returning `(func, args)` is passed as the first argument to the `trampoline()` wrapper, like `trampoline(my_recursive_func)(initial_args)`, and do not attempt to call the returned tuple directly.","cause":"When using the non-decorator `trampoline(func)(...)` style, the recursive function is designed to return a tuple `(func, args)` representing the next call. This error occurs if this returned tuple is mistakenly treated as a callable function.","error":"TypeError: 'tuple' object is not callable"}],"ecosystem":"pypi","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":"0.1.2","cli_name":"","cli_version":null,"type":"library","homepage":"https://gitlab.com/ferreum/trampoline","github":null,"docs":null,"changelog":null,"pypi":"https://pypi.org/project/trampoline/","npm":null,"openapi_spec":null,"status_page":null,"smithery":null,"categories":[],"base_url":null,"auth_type":null,"provenance":{"verified_status":"passing","verified_at":"2026-06-28","last_verified":"2026-08-29","next_check":"2026-07-28","install_tag":null}}