Srinesh
Srinesh

Reputation: 221

What is the purpose of using python `super() `, inside `__init__ ` other than the inheritance?

I found this simple program in a Youtube tutorial which is used QtSide modules with python. Basically what it does is connect QLineEdit to a QTextBrowser. As you can see below, the entire program handled by single class. I have basic idea of super() function which is used in multiple inheritance. So here, I don't understand what super(Form, self).__init__(parent) statement does. I tried running the same program after commenting that statement which produced below error message.

Error:

Traceback (most recent call last):
  File "/home/dazz/Projects/PycharmProjects/FirstTutorial/a2_gui.py", line 35, in <module>
    form = Form()
  File "/home/dazz/Projects/PycharmProjects/FirstTutorial/a2_gui.py", line 17, in __init__
    self.setLayout(layout)
RuntimeError: '__init__' method of object's base class (Form) not called.

Program code:

import sys
from PySide.QtCore import *
from PySide.QtGui import *


class Form(QDialog):
    def __init__(self, parent=None):
        super(Form, self).__init__(parent)

        self.browser = QTextBrowser()
        self.lineEdit = QLineEdit()


        layout = QVBoxLayout()
        layout.addWidget(self.browser)
        layout.addWidget(self.lineEdit)
        self.setLayout(layout)

        self.lineEdit.returnPressed.connect(self.update_ui)
        self.setWindowTitle('Calculate')

    def update_ui(self):
        try:
            text = self.lineEdit.text()
            self.browser.append('%s = %s' % (text, eval(text)))
            self.lineEdit.selectAll()

        except:
            self.browser.append('%s is invalid!' % text)

app = QApplication(sys.argv)
form = Form()
form.show()
app.exec_()

Here, what is the use of super()? I found a question which may related to this. But it's not clear to me.

Upvotes: 2

Views: 1229

Answers (2)

Kaiwen Huang
Kaiwen Huang

Reputation: 786

Basically, super() is used for inheritance, as you already understand. As mentioned by Martjin, the 'consequence' that appeared in your case when you commented away is that there is initialisation required that was already implemented in `QDialog' class.

Hence, all that needs to be done in this child class is to call the parent's init.

There is already a good article in stackoverflow on super and init here.

super and init

Upvotes: 2

Martijn Pieters
Martijn Pieters

Reputation: 1124818

super() is used is inheritance, multiple or otherwise, to call methods that may have been overridden in the current class.

Here super() is used to call the original __init__ method defined for QDialog or a parent class of that. Not calling the original probably will have consequences as that original method does work you don't want to have to replicate in your own __init__ method.

super() makes multiple inheritance easier and more flexible to deal with, but it is not just for multiple inheritance.

Upvotes: 3

Related Questions