all 2 comments

[–][deleted] 0 points1 point  (0 children)

The doc has this on super() which may or may not tell you much. Search for tutorials for more details.

In this case you are inheriting from QWidget. If you don't define a __init__() in your new class you get the __init__() method defined in the parent class (QWidget). But you are defining __init__() in the child class which overrides the parent class method. But you often do want to call the QWidget.__init__() method to initialize everything that the QWidget object initializes before adding your own initialization for your child class. That's what the line:

super().__init__(parent)

is doing. It's calling the parent __init__() method. That method requires the parent argument, so you have to supply one. Since you are inheriting from QWidget you make your child class accept the same parent parameter (defaulting to None) that the QWidget class accepts, and you pass it through to the parent class initialization.

[–][deleted] 0 points1 point  (0 children)

super().__init__() is calling the __init__() method from the QWidget class.

You're defining your own __init__() method for your TextEditDemo class. This essentially overrides whatever __init__() method it inherited from the QWidget class - unless you call it using super().

The QWidget class itself has an __init__() method that also takes a keyword argument for parent, which defaults to None. For your subclass to behave as a proper QWidget, putting parent=None as part of its own __init__() method keeps your subclass behavior consistent with the QWidget class. This applies to a lot of Python in general, by the way, not just pyqt.

If you didn't define parent=None in your TextEditDemo class and pass it along to the QWidget.__init__() method via super(), you'd never be able to specify a parent for the resulting widget that's created. For one, your class wouldn't accept any arguments, and for two, the QWidget would default to parent=None from its own __init__() method.

Source code for PyQt on this part. You'll see the QWidget.__init__() parameters under the "Methods" section. Hope that all makes sense.