[PyQt] Fwd: Fwd: Mixin classes and PyQt4
Baz Walter
bazwal at ftml.net
Wed Feb 12 19:42:48 GMT 2014
On 12/02/14 08:57, Martin Teichmann wrote:
> Hi Baz,
>
>> On the face of it, in your example, how is it possible for B to have
> access to a depth() method
>> when it is not defined in the body of the class? This method is actually
> provided by a Qt mixin
>> (QPaintDevice), but this is obscured by the syntax of vertical
> inheritance. You are viewing the
>> hierarchy from the top down, so all the layers just merge into one
> another.
>
> Well, the big difference is, QWidget inherits from QPaintDevice, from which
> B inherits. For sure,
> B can access depth(). In order to get a similar example, it would be
> necessary for QPaintDevice
> to access methods of QWidget - which it cannot, except QWidget overwrites
> methods which
> QPaintDevice already has. In your version of my example, B does not inherit
> from QObject,
> but still calls its methods. That's what I call weird.
My version of your example is:
class A(object):
def sizeHint(self):
print('mixed in!', self.parent())
return super().sizeHint()
class B(A, QLabel):
pass
so certainly B does inherit QObject (from QLabel). Also, QPaintDevice
certainly would have access to the methods of QWidget *if it was a
python mixin*, and obviously I am talking about the python model of
inheritance here, not C++. In C++, these things may appear "weird", but
in python they are perfectly natural, and commonplace.
It's worth clarifying exactly what is meant by the term Mixin. Let's
take the definition given by Wikipedia:
In object-oriented programming languages, a mixin is a
class which contains a combination of methods from other
classes. How such combination is done depends on language,
*but it is not by inheritance*. [emphasis added]
Later in that article, it uses the ThreadingMixIn class from the
socketserver module to illustrate how mixins are used in python. And if
you look at the source code here:
http://hg.python.org/cpython/file/3.3/Lib/socketserver.py#l596
you will see that the ThreadingMixIn class accesses methods which it
clearly does not inherit (vertically) from any other python class. IOW,
it works in exactly the same way that my class A above works. No
mystery, no weirdness, just standard python usage.
To make it clear: by definition, a mixin class *must* access shared
methods *horizontally*, rather than by vertical inheritance. If it
doesn't, it's not a mixin.
--
Regards
Baz Walter
More information about the PyQt
mailing list