def recursive_function(n, sum): if n it works as much as n=997, then it just breaks and spits out a RecursionError: maximum recursion depth surpassed in comparison. Is this just a stack overflow? Is there a means to get approximately it?


*

*

It is a guard against a stack overflow, yes. Python (or rather, the CPython implementation) doesn"t optimize tail recursion, and also unbridled recursion reasons stack overflows. You can inspect the recursion limit through sys.getrecursionlimit:

import sysprint(sys.getrecursionlimit())and change the recursion limit with sys.setrecursionlimit:

sys.setrecursionlimit(1500)but doing for this reason is dangerous -- the traditional limit is a tiny conservative, yet Python stackframes have the right to be rather big.

You are watching: Recursionerror: maximum recursion depth exceeded in comparison

Python isn"t a practical language and tail recursion is not a particularly efficient technique. Rewriting the algorithm iteratively, if possible, is typically a far better idea.


re-publishing
improve this answer
follow
edited Jul 5 "20 at 15:05
*

rob Bednark
21.3k1818 gold badges7474 silver badges104104 bronze title
answer Jul 23 "10 in ~ 23:08
*

thomas WoutersThomas Wouters
122k2121 gold badges141141 silver badges118118 bronze badges
10
| show 5 much more comments
152
Looks favor you simply need to set a higher recursion depth:

import syssys.setrecursionlimit(1500)
re-superstructure
enhance this prize
monitor
edited Nov 17 "19 in ~ 22:41

*

Boris
8,25177 yellow badges7171 silver- badges6969 bronze badges
reply Jul 23 "10 at 23:07
David YoungDavid Young
4,33322 yellow badges1919 silver- badges1818 bronze title
2
add a comment |
60
It"s to protect against a ridge overflow. The Python interpreter borders the depths of recursion to aid you avoid unlimited recursions, leading to stack overflows.Try boosting the recursion border (sys.setrecursionlimit) or re-writing your password without recursion.

From the Python documentation:

sys.getrecursionlimit()

Return the current value the the recursion limit, the maximum depth of the Python interpreter stack. This limit avoids infinite recursion from bring about an overflow that the C stack and crashing Python. It can be collection by setrecursionlimit().


re-publishing
boost this price
follow
edited Jun 20 "20 at 9:12
CommunityBot
111 silver badge
answer Jul 23 "10 at 23:08
ScharronScharron
15.9k66 gold badges4141 silver badges6363 bronze title
1
include a comment |
45
If you regularly need to change the recursion border (e.g. While solving programming puzzles) you can define a straightforward context manager like this:

import sysclass recursionlimit: def __init__(self, limit): self.limit = border self.old_limit = sys.getrecursionlimit() def __enter__(self): sys.setrecursionlimit(self.limit) def __exit__(self, type, value, tb): sys.setrecursionlimit(self.old_limit)Then to call a duty with a practice limit you have the right to do:

with recursionlimit(1500): print(fib(1000, 0))On departure from the body of the v statement the recursion limit will certainly be restored to the default value.


re-superstructure
boost this price
follow
answered may 1 "18 at 16:37
Eugene YarmashEugene Yarmash
125k3535 yellow badges288288 silver- badges348348 bronze title
1
add a comment |
22
resource.setrlimit must likewise be supplied to increase the ridge size and prevent segfault

The Linux kernel limits the ridge of processes.

Python stores neighborhood variables ~ above the stack of the interpreter, and so recursion takes increase stack space of the interpreter.

If the Python interpreter do the efforts to go over the stack limit, the Linux kernel makes it segment fault.

The ridge limit dimension is managed with the getrlimit and setrlimit mechanism calls.

Python offers accessibility to those mechanism calls through the source module.

sys.setrecursionlimit pointed out e.g. At https://tastecraftedmcd.com/a/3323013/895245 only increases the limit the the Python interpreter self imposes top top its very own stack size, but it does no touch the limit implemented by the Linux kernel top top the Python process.

See more: Town And Country Vet Christiansburg Va 24073, Town & Country Veterinary Clinic, P

Example program:

main.py

import resourceimport sysprint resource.getrlimit(resource.RLIMIT_STACK)print sys.getrecursionlimit()print# will certainly segfault without this line.resource.setrlimit(resource.RLIMIT_STACK, <0x10000000, resource.RLIM_INFINITY>)sys.setrecursionlimit(0x100000)def f(i): publish i sys.stdout.flush() f(i + 1)f(0)Of course, if friend keep increasing setrlimit, your lamb will at some point run out, which will certainly either sluggish your computer system to a halt as result of swap madness, or kill Python via the OOM Killer.