Skip to main content

This module helps you to get rid of recursions when you wantto rename a builtin function, or a function that already exists.

Project description

This module helps you to get rid of recursions when you want to rename a builtin function, or a function that already exists.

usage:

from no_recursion import no_recursion, replace

@no_recursion
@replace(bin)
def bin(n: int):
    return bin(n)

The decorator replace must be below no_recursion.

it will replace each recursive call to bin with a call to the "original" bin.

You can also set a namespace where it will try to find the original function in,

the original function must be named the same as the decorated function.

You can specify a particular namespace in @no_recursion.

from no_recursion import no_recursion, replace

@no_recursion(vars(__builtins__))
def bin(n: int):
    return bin(n)

In this example, it will take the builtin bin.

The namespace must be a dict that contains the function.

You can also use decorated_f.replace (not the global replace function) to set a replacement function.

import builtins
from no_recursion import no_recursion, replace


@no_recursion
def bin(n: int):
    return bin(n)


@bin.replace
def replace_bin(n: int):
    return builtins.bin(n)

All examples above will produce the same result.

print(bin(12))
# 0b1100

NOTES:

when you call no_recursion with parentheses, you must specify a namespace (@no_recursion({}). Otherwise just do @no_recursion. Exception can be raised.

The decorator replace must be below no_recursion.

In the namespace dictionary, the original function must be named the same as the decorated function.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

no-recursion-1.0.3.tar.gz (2.9 kB view hashes)

Uploaded Source

Built Distribution

no_recursion-1.0.3-py3-none-any.whl (3.2 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page