0

I am creating a multilevel inheritance. I am in doubt why my base classes methods in constructor (__init__) are not getting calling only super is executing.

Here is my example.

from PyQt5.QtWidgets import QMainWindow

class PSheet(object):

    def __init__(self):

        print('PSheet - Start')
        super(PSheet, self).__init__()
        print('PSheet - End')

        self.some_method()

    def some_method(self):

        print('PSheet - In Some Method')

class config(object):

    def __init__(self):
        print('config - Start')
        super(config, self).__init__()
        print('config - End')

class BaseClassWithQt(QMainWindow, config):

    def __init__(self):
        print('BaseClassWithQt - Start')
        super(BaseClassWithQt, self).__init__()
        print('BaseClassWithQt - End')

class DerivedClassWithQt(BaseClassWithQt, PSheet):

    def __init__(self):
        print('DerivedClassWithQt - Start')
        super(DerivedClassWithQt, self).__init__()
        print('DerivedClassWithQt - End')


print(DerivedClassWithQt.__mro__)
test_with_qt = DerivedClassWithQt()

Output

(<class '__main__.DerivedClassWithQt'>, <class '__main__.BaseClassWithQt'>, <class '__main__.config'>, <class '__main__.PSheet'>, <class 'object'>)
DerivedClassWithQt - Start
BaseClassWithQt - Start

I am confused what wrong i am doing, since i am not getting all the prints available in all constructors.

For clarification i have printed mro also for both cases.

What do i need to correct?

Rao
  • 2,902
  • 14
  • 52
  • 70
  • Your question looks a lot like other questions on multiple inheritance (e.g. https://stackoverflow.com/questions/9575409/calling-parent-class-init-with-multiple-inheritance-whats-the-right-way). Qt has nothing to do with it? – Demi-Lune Apr 12 '21 at 13:14

1 Answers1

0

This is the full output I'm getting when I run your code (note that in the future you should try and include the full output in the question):

(<class '__main__.DerivedClassWithQt'>, <class '__main__.BaseClassWithQt'>, <class 'PyQt5.QtWidgets.QMainWindow'>, <class 'PyQt5.QtWidgets.QWidget'>, <class 'PyQt5.QtCore.QObject'>, <class 'sip.wrapper'>, <class 'PyQt5.QtGui.QPaintDevice'>, <class 'sip.simplewrapper'>, <class '__main__.config'>, <class '__main__.PSheet'>, <class 'object'>)
DerivedClassWithQt - Start
BaseClassWithQt - Start
QWidget: Must construct a QApplication before a QWidget
Aborted (core dumped)

So, I added just the app declaration to the bottom of the file before the initiation of test_with_qt, which you'll need to have later in the program regardless:

app = QApplication([])

# print(DerivedClassWithQt.__mro__)
test_with_qt = DerivedClassWithQt()

This then gives me the following output (without the mro):

DerivedClassWithQt - Start
BaseClassWithQt - Start
config - Start
PSheet - Start
PSheet - End
PSheet - In Some Method
config - End
BaseClassWithQt - End
DerivedClassWithQt - End

As a sidenote, you can save some writing by using parameter-less super, as it is equivalent to what you have written. For example with the BaseWithQt class, and all other classes, all you need is:

super().__init__()
Rolv Apneseth
  • 2,078
  • 2
  • 7
  • 19
  • 1
    As you suggested parameter-less super, is working the same way. Thanks for the suggestion. Also without app=QApplication([]) i am not getting the error that you have mentioned. But after keeping app=QApplication([]), i am able to get all prints from all constructors – Rao Apr 13 '21 at 04:14