1

Code snippet:

from PyQt5.QtWidgets import QApplication
from PyQt5.QtCore import QObject
import sys

class ParentA:
    def __init__(self, x, y):
        self.b = x + y

class Child(QObject, ParentA):
    def __init__(self, x, y):
        QObject.__init__(self) # <- Error is thrown here
        z = x + 2
        ParentA.__init__(self, z, y)
        self.c = "Child specific value"
        print("end")


if __name__ == "__main__":
    app = QApplication([])
    Child(1,2)
    sys.exit(app.exec_())

Error message:

Exception has occurred: TypeError ParentB.init() missing 2 required positional arguments: 'x' and 'y'

Error thrown on first line of Child constructor

Using VS code on windows

When you replace QObject with a self-made class, the problem does not appear. Also if you do not call the QObject init function in the Child constructor, there is no problem, but I believe there is no reason why I should refrain from calling the QObject constructor.

2
  • 3
    It seems to be an implementation detail specific to PyQt. The same example does not raise an exception with PySide. For PyQt, if you change the definition to class Child(ParentA, QObject) the problem will go away. But really, you should be using super for cases like this. See the PyQt docs: Support for Cooperative Multi-inheritance. Commented Mar 15, 2024 at 15:40
  • The error message is quite clear on what is wrong. It also didn't happen with this snippet of code, since it mentions a ParentB class. Commented Jul 6, 2024 at 4:51

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.