0

I created a very short program that forces the "RecursionError: maximum recursion depth exceeded while pickling an object" to occur

import sys
sys.setrecursionlimit(1)

AB,CD=0,0

def A():
    global AB
    AB+=1
    print('A',AB,CD)
    # End all () except for A
    B()
    print('A2')

def B():
    global AB
    AB+=1
    print('B')
    C()
    print('B2')

def C():
    global CD
    CD+=1
    print('C')
    D()
    A()
    print('D2')

def D():
    global CD
    CD+=1
    print('D')

A()

Screenshot of Code

What you will get in shell is:

A 1 0
B
C
#<...> (a couple of lines that I cut out to make the post clearer, screenshot below)
C
D
A 9 8
Traceback (most recent call last):
  File "D:!Z pulpituPythonVTX RE.py", line 34, in <module>
    A()
  File "D:!Z pulpituPythonVTX RE.py", line 11, in A
    B()
  File "D:!Z pulpituPythonVTX RE.py", line 18, in B
    C()
#<...> (a couple of lines that I cut out to make the post clearer, screenshot below)
  File "D:!Z pulpituPythonVTX RE.py", line 11, in A
    B()
  File "D:!Z pulpituPythonVTX RE.py", line 17, in B
    print('B')
RecursionError: maximum recursion depth exceeded while pickling an object

Screenshot of Error

The question is: Can I somehow end/exit/quit/close/forget all the iterations of B(), C() and D() when I’m in A() (more specifically where "# End all () except for A" is located) without resetting the values AB and CD? And will it solve the RecursionError?

Cause as far as I’m aware, while D() ends itself cause it doesn’t call any other recursion, B() and C() are still running in the background waiting for their called functions to finish so they can get to print(‘B2’) and print(‘C2’) respectively, taking unnecessary memory (which, with higher recursionlimit and more complicated objects can cause a Memory Error)

Anonymous Changed status to publish May 14, 2021