Python Forum
RecursionError: maximum recursion depth exceeded in comparison ? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: RecursionError: maximum recursion depth exceeded in comparison ? (/thread-16790.html)

Pages: 1 2


RE: RecursionError: maximum recursion depth exceeded in comparison ? - micseydel - Mar-15-2019

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.


RE: RecursionError: maximum recursion depth exceeded in comparison ? - leoahum - Mar-18-2019

(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.