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 belowno_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 belowno_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
Built Distribution
Hashes for no_recursion-1.0.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0582d8eae5f0f5f7651ae6701b232544d95b70b2045d0df2fbfe3909dbde5878 |
|
MD5 | e64af4fbe371acbe672c37b146eba8e7 |
|
BLAKE2b-256 | b44a67734353c94820f6a96c79ade0cf33e528cb2189bd42ba382ac6e757b665 |