Skip to main content

precursion PyPI Python 2.7, 3.4, 3.5, 3.6

precursion – Python module to avoid RecursionError: maximum recursion depth exceeded easily

Usage

Ok, let's write recursive function:

def sumrange(x):
    if x == 0:
        return 0

    r = sumrange(x - 1)
    return x + r

print(sumrange(10)  # 55

Pretty simple. But what if we call it with bigger argument

print(sumrange(1000))
# RecursionError: maximum recursion depth exceeded

Let's fix it with precursion module:

@precurse
def sumrange(x):
    if x == 0:
        # return was:
        # return 0
        # now we need to use StopIteration exception:
        raise StopIteration(0)

    # recursive call was:
    # r = sumrange(x - 1)
    # now we use yield:
    r = yield sumrange.r(x - 1)
    raise StopIteration(x + r)

print(sumrange(1000))  # 500500!!1

That's it!

What is .r in sumrange.r?

It's unwrapped function, so you yield unwrapped generator

Pros and cons:

Pros

The code looks cleaner. Yep.

Cons

Function calls have performance and memory overhead, so using the decorator is slower than if you use Tail-call optimization via while or implement a stack inside a function.

Metadata

Release files for precursion 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for precursion 1.0.0
File Size Uploaded Provenance Actions
precursion-1.0.0.tar.gz 2.4 kB Without provenance Details for precursion-1.0.0.tar.gz

Release files / precursion-1.0.0.tar.gz

Download URL precursion-1.0.0.tar.gz
Size 2.4 kB
Tags Source
SHA-256 checksum
How to use checksums
ba750d0f63ef9820abc40bd0ac2384d3c23eca656d4880ac4af8a22b1d467a86
BLAKE2b-256 checksum
How to use checksums
5d22a252602a16ca6db0b4c31a155ca4ad9b765960b69853070581859091add2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for precursion 1.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.0.0 This release

1 release file No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page