Python Forum
RecursionError: maximum recursion depth exceeded in comparison ?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
RecursionError: maximum recursion depth exceeded in comparison ?
I should probably not have assumed that you are familiar with mutability in Python. Basically, when you use a list in a default argument, it's always the same list, which can be edited (as opposed to a tuple). Because it's always the same list, edits to that "leak" in the sense that changes you might not have expected will worm into things.
Feel like you're not getting the answers you want? Checkout the help/rules for things like what to include/not include in a post, how to use code tags, how to ask smart questions, and more.

Pro-tip - there's an inverse correlation between the number of lines of code posted and my enthusiasm for helping with a question :)
(Mar-15-2019, 08:39 PM)ichabod801 Wrote: Again, you are not being clear. We need the full text of the error you are getting.

Here is the full error text message.

Traceback (most recent call last):
File "C:/Users/hali/Downloads/", line 126, in <module>
L.append(MoveAcorHeight(Lx[i], Ly[i], xgap, ygap, H[i]))
File "C:/Users/hali/Downloads/", line 31, in MoveAcorHeight
return MoveAcorHeight(lx, ly, xgap, ygap, H, itn, K, itn2)
File "C:/Users/hali/Downloads/", line 103, in MoveAcorHeight
return MoveAcorHeight(lx, ly, xgap, ygap, H, itn, K, itn2)
File "C:/Users/hali/Downloads/", line 40, in MoveAcorHeight
IndexError: list index out of range

(Mar-15-2019, 10:56 PM)micseydel Wrote: I'm taking a guess at your issue... Default function parameters are only evaluated once, at function definition. Take this example:
>>> def f(x=[]): 
...   x.append(True)
...   return x
>>> f()
>>> f()
[True, True]
>>> f()
[True, True, True]
Here's the idiom for getting around that issue
>>> def g(x=None):
...   if x is None: 
...     x = []
...   x.append(True)
...   return x
>>> g()
>>> g()
>>> g()
Also, an aside, if you can reproduce your problem in 5-10 lines of code (possible here) that tends to be preferable to longer code and makes it more likely that you get an answer which is both as fast as possible and satisfactory.

Thanks! Problem solved. It was a genius guess!

BTW,I did try to reproduce my code in a shorter version when I started this thread, but that version failed to reflect the facts. Then I failed to reproduce another version.

Possibly Related Threads…
Thread Author Replies Views Last Post
  Time Limit Exceeded error loves 5 675 Dec-03-2020, 07:15 AM
Last Post: Sofia_Grace
Bug maximum recursion depth exceeded while calling a Python object error in python3 Prezess 4 1,632 Aug-02-2020, 02:21 PM
Last Post: deanhystad
  Requesting help with my implementation of depth-first search tigerfuchs 6 970 Sep-26-2019, 05:47 AM
Last Post: perfringo
  Pyinstaller Maximum recursion bug scales11 7 6,614 Apr-10-2019, 07:06 PM
Last Post: scales11
  fibonacci ***Time limit exceeded*** frequency 18 4,373 Nov-29-2018, 09:03 PM
Last Post: frequency
  'Time Limit Exceeded' Problem bkpee3 2 2,743 Nov-14-2018, 03:51 AM
Last Post: bkpee3
  Why I get RecursionError on very small amount of data? wavic 3 1,780 Aug-05-2018, 04:55 PM
Last Post: micseydel
  variable loop depth Skaperen 5 2,158 Jul-18-2018, 02:48 AM
Last Post: Skaperen
  maximum recursion depth exceeded saba_keon 3 4,451 Apr-08-2018, 07:30 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020