1
class SimpleRoundedCorners(QWidget):
    def __init__(self):
        super(SimpleRoundedCorners, self).__init__()
        self.initUI()

This makes no sense to me. I've read how super() works, and maybe I'm just not good enough at objects to grasp it. This seems like a very redundant call. Why am I calling init twice on the same object?

kasuals
  • 11
  • 1
  • 3
    If you translate this code to English, it says something like *"a `SimpleRoundedCorners` is a kind of `QWidget`, and to initialise one you need to do the same things required to initialise a `QWidget` and also call its `initUI` method."* There is no `__init__` method here that gets called twice; there are two `__init__` methods and one calls the other. – kaya3 Mar 06 '21 at 06:08
  • @kaya3 Nice one! – Omar AlSuwaidi Mar 06 '21 at 06:27
  • @kaya3 That was a great explanation as well. I wasn't thinking about it that way! – kasuals Mar 06 '21 at 18:01
  • Does this answer your question? [What does 'super' do in Python? - difference between super().\_\_init\_\_() and explicit superclass \_\_init\_\_()](https://stackoverflow.com/questions/222877/what-does-super-do-in-python-difference-between-super-init-and-expl) – Bharel Dec 22 '21 at 20:23

1 Answers1

1

In OOP, super() is very useful for the purpose of multiple-inheritance. It gives you access to methods and attributes from a superclass, to be used in a sub/child class, which saves a lot of time and effort.

The second init call is used to access the superclass from the base/sub class. Where you pass in the arguments required by the superclass's init constructor; such that when you instantiate the base/sub class, you only pass in the arguments required by its init constructor only, not the arguments required for the superclass's init constructor. A nice example of that is looking at the implementation in this answer.

Moreover, you can just write super().__init__() without explicitly referring to the base class.

Omar AlSuwaidi
  • 1,187
  • 2
  • 6
  • 26
  • 1
    Thank you. After re-reading the super() definition with your explanation and link it makes total sense. – kasuals Mar 06 '21 at 06:21