[PyQt] Fwd: Mixin classes and PyQt4

Martin Teichmann martin.teichmann at gmail.com
Tue Feb 11 10:29:31 GMT 2014


Hi Phil, Hi Baz, Hi List,

to pitch in my two cents:

yes, I can just write:

class A(object):
   def sizeHint(self):
        print(self.parent())
        super().sizeHint()

class B(A, QLabel):
   pass

it works OK, but it is actually really weird code.
In class A I am calling methods which are not in my base class.
Well, it still works, so I shouldn't complain (I simply did
not get the idea of writing that, since, well, what is it
supposed to mean to call a method of a, well, non-existing
base class?)

Yes I know, I am just nitpicking, it would just be cool to be
able to write nice code... I tried to find a situation in which this
triggers a problem, couldn't find one yet but well. So, this
is why I think my patch is still cool, as it allows for nicer code.

Btw, the discussion whether class A(B, C), or class A(C, B) is
prettier is, well, weird. This is like discussing whether 2 ** n or
n ** 2 is prettier. Neither is, it's just different code.

That said, it should be stated clearly somewhere in the PyQt
documentation: ALLWAYS PUT YOUR Qt CLASS LAST IN THE
INHERITANCE LIST! Why that? Well, PyQt is not cooperative,
meaning it is not calling super, so it does not honor the method
resolution order. By putting the Qt classes last in the inheritance
list, all Qt classes will always end up in the end of the MRO, so
be called last. Then it simply doesn't matter that they in turn
don't call super, since, well, the MRO is at its end anyways.

What you don't ever want is to have is a situation in which a
non-Qt class ends up in the middle of the MRO chain. An example
would be:

class Mixin(QObject):
  pass

class B(QWidget, Mixin):
  pass

Then the MRO is B, QWidget, Mixin, QObject. You must be
really lucky to get your methods in Mixin called properly.

In PyQt5, I read, __init__ does call super. While this might
be nice, it is actually unnecessary. The example in the docs
(http://pyqt.sourceforge.net/Docs/PyQt5/multiinheritance.html)
actually works perfect with PyQt4 already if one just swaps
QObject and Age in the class definition of Person.

Greetings

Martin
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20140211/17c46bfd/attachment.html>


More information about the PyQt mailing list