简体   繁体   中英

Python3 super().__init__() maximum recursion depth reached

I'm having a multiple inheritance problem in python. Using python's super() command is giving me a "maximum recursion depth reached". The code:

class program(object):
    def back(self):
    if self.currentWin == "welcome":
        self.welcomeWindow.root.destroy()
        exit(0)
    def login(self): print("login")
    def register(self): print("register")
    def __init__(self):
        super().__init__()
        self.currentWin = "welcome"
        self.welcomeWindow = welcomewin()

class welcomewin(program):
    def __init__(self):
        super().__init__()
        self.root=tkinter.Tk()
        tkinter.Button(self.root,text="Exit",command=self.back).grid(row=1,column=2)
if __name__=="__main__": program()

The problem here is I'm trying to have a button inside welcomewin that calls program's back function, which it has inherited from program. However, it has not inherited the currentwin variable that back() relies on in order to decide what to do. I get the following error if I run this code:

  File "E:/Python project/test.py", line 15, in __init__
    super().__init__()
  File "E:/Python project/test.py", line 11, in __init__
    self.welcomeWindow = welcomewin()
  File "E:/Python project/test.py", line 15, in __init__
    super().__init__()
  File "E:/Python project/test.py", line 11, in __init__
    self.welcomeWindow = welcomewin()
  File "E:/Python project/test.py", line 15, in __init__
    super().__init__()
  File "E:/Python project/test.py", line 11, in __init__
    self.welcomeWindow = welcomewin()
RuntimeError: maximum recursion depth exceeded while calling a Python object
>>> 

It is only logical that you get this error. You inherit welcomewin from program and create a new welcomewin object in the __init__() function of program . This means a new welcomewin object will be created that calls the __init__() function again and this goes on forever.

Easy solution: Don't inherit welcomewin from program.

You are calling init in init that's creating an infinite loop.

def __init__(self):
    super().__init__()

This should work

def __init__(self):
    super(program)

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