Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
RecursionError: maximum recursion depth exceeded in comparison ?
#11
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.
Quote
#12
(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/11.py", line 126, in <module>
L.append(MoveAcorHeight(Lx[i], Ly[i], xgap, ygap, H[i]))
File "C:/Users/hali/Downloads/11.py", line 31, in MoveAcorHeight
return MoveAcorHeight(lx, ly, xgap, ygap, H, itn, K, itn2)
File "C:/Users/hali/Downloads/11.py", line 103, in MoveAcorHeight
return MoveAcorHeight(lx, ly, xgap, ygap, H, itn, K, itn2)
File "C:/Users/hali/Downloads/11.py", line 40, in MoveAcorHeight
PrebsX[K[i][0]].append(lx[i])
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()
[True]
>>> 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()
[True]
>>> g()
[True]
>>> g()
[True]
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.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Replace with Maximum Value leoahum 4 119 Mar-13-2019, 06:24 PM
Last Post: leoahum
  Getting the maximum point of the graph from the text file Erfans 2 124 Mar-09-2019, 11:01 PM
Last Post: Erfans
  The Empty List Blocks Recursion leoahum 6 95 Mar-05-2019, 06:55 PM
Last Post: leoahum
  Problem with comparison operator mdrisser 2 87 Feb-28-2019, 09:45 PM
Last Post: mdrisser
  function call in recursion moong 2 151 Feb-17-2019, 09:48 AM
Last Post: moong
  Cheat against recursion limit DeaD_EyE 2 153 Feb-10-2019, 10:44 AM
Last Post: DeaD_EyE
  iterate through a list with comparison of expression Alexandro 6 187 Jan-31-2019, 05:16 PM
Last Post: Scorpio
  Turning Robot towards goal, comparison of angles SpaceTime 0 108 Jan-21-2019, 10:26 PM
Last Post: SpaceTime
  How recursion function is applied? dukoolsharma 2 195 Dec-06-2018, 12:35 PM
Last Post: himanibansal
  fibonacci ***Time limit exceeded*** frequency 18 640 Nov-29-2018, 09:03 PM
Last Post: frequency

Forum Jump:


Users browsing this thread: 1 Guest(s)