简体   繁体   中英

Error while running 'classloop'

i was messing around with classes and thought i could try and make a class just loop

here is what i did:

class A():
    def __init__(self):
        print 1
        self.loop()

    def loop(self):
        print 2
        self.__init__()
A()

it prints 1 & 2 back and fourth for a while then i get a error that starts looping that looks like this:

Traceback (most recent call last):
  File "C:/Python27/classloop.py", line 12, in <module>
    A()

then it starts looping these two errors back and fourth:

File "C:/Python27/classloop.py", line 4, in __init__
    self.loop()
File "C:/Python27/classloop.py", line 9, in loop
    self.__init__()

just wondering why this happens all of the sudden why doesnt it just keep looping?

Every function call creates a stack frame, and every return pops a frame off the stack. This means that if you recurse too deep without returning, Python will run out of room on the stack and throw an exception. You can increase the limit, but most of the time, this will only make your program run a bit longer before crashing, or worse, the Python interpreter will corrupt its memory and go crazy.

In python there is a maximum recursion limit. The default is 1000. You can see that by typing:

import sys
print sys.getrecursionlimit()

in the terminal.

If you want to increase it use:

sys.setrecursionlimit(10000) # 10000 is just an example

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM