#!/usr/bin/python
import sys
from PyQt4 import QtGui
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__() # what the meaning of this line
self.initUI()
def initUI(self):
lbl1 = QtGui.QLabel('ZetCode', self)
lbl1.move(15, 10)
lbl2 = QtGui.QLabel('tutorials', self)
lbl2.move(35, 40)
lbl3 = QtGui.QLabel('for programmers', self)
lbl3.move(55, 70)
self.setGeometry(300, 300, 250, 150)
self.setWindowTitle('Absolute')
self.show()
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
Asked
Active
Viewed 2,752 times
0

jonrsharpe
- 115,751
- 26
- 228
- 437

Tigran84
- 193
- 3
- 16
-
Or any of the dozens of other questions on `super` within and without `__init__` already on SO - please do some research before asking. – jonrsharpe Nov 16 '15 at 11:13
-
All you really need to know is that, in your example, it's more or less equivalent to `QWidget.__init__(self)` (which you can easily test for yourself). When an instance of a subclass is created, python will automatically call the `__init__` of the baseclass - except when you've overridden it, as in your example. In general, if you override `__init__` in a subclass, you need to explicitly call the baseclass `__init__` as well. There are other reasons for specifically using `super` to do this, but, strictly speaking, they don't apply so much in PyQt, because it mostly uses single inheritance. – ekhumoro Nov 16 '15 at 17:03
1 Answers
0
super() function invokes the parent constructor. In case of earlier version of python, we have to manually decide the parent class whose constructor have to be invoked, in case when child class inherits from multiple class. In new style classes, super() does this task to avoid diamond problem on inheritance. Refer to super() doc in python documentation.

Mangu Singh Rajpurohit
- 10,806
- 4
- 68
- 97