Here's the traceback:
Traceback (most recent call last):
File "/home/neon/pyqt6/myqt/src/app.py", line 13, in <module>
window = AppWindow()
^^^^^^^^^^^
File "/home/neon/pyqt6/myqt/src/app.py", line 7, in __init__
super(AppWindow, self).__init__()
File "/home/neon/pyqt6/myqt/src/ui_mainwindow.py", line 5, in __init__
print(self)
RuntimeError: '__init__' method of object's base class (AppWindow) not called.
I'm trying to develop a personal QT6 based app in Python3.11. At some point I got stuck somewhere and decided to print(self)
in one of the parents of a subclass, and that's what caused the above runtime error.
I've seen the duplicate. I have made attempts below to show why this runtime error should not happen in my opinion.
Here's how to reproduce the error:
- Install
PySide6
module in a virtual environment - Make file
ui_mainwindow.py
with following content:
from PySide6.QtWidgets import QLineEdit
class Ui_MainWindow(object):
def __init__(self):
# This call causes error
print(self)
# A sample widget. Irrelevant for error
def makeui(self):
self.line_edit = QLineEdit()
- Make file
app.py
with following content:
from PySide6.QtWidgets import QMainWindow, QApplication
from ui_mainwindow import Ui_MainWindow
import sys
class AppWindow(QMainWindow, Ui_MainWindow):
def __init__(self):
super(AppWindow, self).__init__()
self.makeui()
self.setCentralWidget(self.line_edit)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = AppWindow()
window.show()
app.exec()
- Run app.py as
python app.py
and see the error reproduced.
Here's something unexpected. If I replace the call print(self)
with anything else, let's say print(self.__class__.mro())
or print("Hello World")
then no error occurs and print call works!
I'm unable to make sense of why print(self)
is causing runtime error with that message.
I also tried to replicate the problem without qt6 and with only python based objects. Here's what I also tried and no error occurred even when print(self)
was called in one parent's constructor.
- Contents of main.py
from class_d import D
class A(object):
def __init__(self):
super().__init__()
class B(A):
def __init__(self):
super().__init__()
class C(B):
def __init__(self):
super().__init__()
class E(C, D):
def __init__(self):
super(E, self).__init__()
e = E()
- Contents of class_d.py
class D(object):
def __init__(self):
print(self) # this call works as expected
Doing python main.py
happily prints the repr of E object.
So why is that when qt objects are involved I am getting runtime error?