[PyQt] Fwd: Mixin classes and PyQt4

Baz Walter bazwal at ftml.net
Tue Feb 11 19:31:27 GMT 2014


On 11/02/14 10:29, Martin Teichmann wrote:
> Hi Phil, Hi Baz, Hi List,

Hi Martin

> 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?)

Is it that weird, really?

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.

A mixin is part of a deconstructed subclass. The layer containing the 
additional and reimplemented members of the subclass (which would 
normally be defined in its body) is detached from the top of the 
vertical hierarchy and arranged horizontally instead. And in the 
horizontal dimension of inheritance, classes are ordered left to right - 
so naturally the A mixin of your example must go to the left of the 
QLabel base-class. It can never make any sense for it to go on the right 
(that would be like saying B somehow belongs at the bottom of the 
vertical hierarchy).

> 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.

If you have no real use-case, please define what "cool" and "nice" mean 
in this context.

> 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.

Totally agree with you here.

-- 
Regards
Baz Walter


More information about the PyQt mailing list