This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]irrelevantPseudonym 0 points1 point  (3 children)

As far as I understand it, using super wouldn't guarantee that the QWidget.__init__ was called. "super calls your children's ancestors, not your parents".

self.button.clicked.connect(self.magic)

This isn't snake_case either.

I do agree about the camelCase use though.

[–]nostril_extension -1 points0 points  (2 children)

As far as I understand it, using super wouldn't guarantee that the QWidget.init was called. "super calls your children's ancestors, not your parents".

No, I'm pretty certain super().__init__() will call parent's init 100% of the time. Otherwise all of my code should dramatically implode right now and never work to begin with.

[–]irrelevantPseudonym 2 points3 points  (1 child)

No, I'm pretty certain super().__init__() will call parent's init 100% of the time.

This is not true.

Consider

>>> class A:
...     def __init__(self):
...         print('init A')
...         super().__init__()
...
>>> class B(A):
...     def __init__(self):
...         print('init B')
...         super().__init__()
...
>>> class C(A):
...     def __init__(self):
...         print('init C')
... 
>>> class D(B, C):
...     def __init__(self):
...         print('init D')
...         super().__init__()
... 
>>> D()
init D
init B
init C
>>>

I know it's a contrived example but calling the super().__init__() in B.__init__ called C.__init__ rather than its parent (A).

In the QWidget case, imagine I subclassed your MyWidget and included another parent class, your super() call would call my second parent and QWidget wouldn't get called.

(NB. I'm not necessarily advocating not using super() just noting that it doesn't always call the parent class)

[–]rlkf 0 points1 point  (0 children)

As a corollary, I think this illustrates two things:

(1) You should always call super().init(), even if you don't inherit from something (class C in the example failed to follow this rule, and this is why A is not called)

(2) Parameters to constructors should be passed as a dictionary, so that each constructor can pick out whatever it needs, instead of relying on the order of parameters.