[prev in list] [next in list] [prev in thread] [next in thread] 

List:       pykde
Subject:    [PyQt] Antwort: Re:  Multiple Inheritance with PyQt5 on python 2
From:       Sebastian Eckweiler <sebastian.eckweiler () twt-gmbh ! de>
Date:       2016-12-12 9:17:52
Message-ID: OFB77ADC95.2B673E4D-ONC1258087.00319F5A-C1258087.003317D3 () twt-gmbh ! de
[Download RAW message or body]

Dies ist eine mehrteilige Nachricht im MIME-Format.

Dies ist eine mehrteilige Nachricht im MIME-Format.
--=_alternative 003317D3C1258087_=
Content-Type: text/plain; charset="US-ASCII"

Hi there, 

thanks - I guess I wasn't really aware of the super-workings with multiple 
inheritance (a good occasion to re-read Hettinger's nlog entry on 
this...).
Things worked just fine though with PyQt4, so this caused some confusion.

Cheers
Sebastian



Von:    Baz Walter <bazwal@ftml.net>
An:     pyqt@riverbankcomputing.com
Datum:  09.12.2016 20:51
Betreff:        Re: [PyQt] Multiple Inheritance with PyQt5 on python 2
Gesendet von:   "PyQt" <pyqt-bounces@riverbankcomputing.com>



On 09/12/16 12:22, Sebastian Eckweiler wrote:
> Hi there,
> 
> I've just read through
> http://pyqt.sourceforge.net/Docs/PyQt5/multiinheritance.html.
> I'm using a self-built PyQt5 for Python 2 on Windows - and it seems I
> can't get multiple inheritance to work:
> 
> class SomeClass(object):
> 
> def __init__(self, attr):
> self.attr = attr
> 
> class AnotherDialog(QtWidgets.QDialog, SomeClass):
> 
> def __init__(self, parent=None):
> #super(AnotherDialog, self).__init__(parent) # tested this was
> well
> QtWidgets.QDialog.__init__(self, parent)
> SomeClass.__init__(self, 1)
> 
> 
> app = QtWidgets.QApplication([])
> dlg = AnotherDialog()
> 
> This always results in
> 
> TypeError: __init__() takes exactly 2 arguments (1 given)
> 
> is there a workaround for this?

The signature of SomeClass has one required positional argument. If you 
use super, how is it going to get that argument if you don't pass it to 
the AnotherDialog constructor?

The docs you linked to explained that you should use keyword arguments. 
So one way to resolve the issue would be like this:

     class SomeClass(object):
         def __init__(self, attr=1, **kwargs):
             super(SomeClass, self).__init__(**kwargs)
             self.attr = attr

     class AnotherDialog(QtWidgets.QDialog, SomeClass):
         def __init__(self, parent=None, **kwargs):
             super(AnotherDialog, self).__init__(parent, **kwargs)

Or, less flexibly, perhaps like this:

     class SomeClass(object):
         def __init__(self, attr):
             super(SomeClass, self).__init__()
             self.attr = attr

     class AnotherDialog(QtWidgets.QDialog, SomeClass):
         def __init__(self, parent=None):
             super(AnotherDialog, self).__init__(parent, attr=1)

Without super, put the mixin first:

     class SomeClass(object):
         def __init__(self, attr):
             self.attr = attr

     class AnotherDialog(SomeClass, QtWidgets.QDialog):
         def __init__(self, parent=None):
             SomeClass.__init__(self, 1)
             QtWidgets.QDialog.__init__(self, parent)

_______________________________________________
PyQt mailing list    PyQt@riverbankcomputing.com
https://www.riverbankcomputing.com/mailman/listinfo/pyqt

--=_alternative 003317D3C1258087_=
Content-Type: text/html; charset="US-ASCII"

<font size=2 face="sans-serif">Hi there, </font>
<br>
<br><font size=2 face="sans-serif">thanks - I guess I wasn't really aware
of the super-workings with multiple inheritance (a good occasion to re-read
Hettinger's nlog entry on this...).</font>
<br><font size=2 face="sans-serif">Things worked just fine though with
PyQt4, so this caused some confusion.</font>
<br>
<br><font size=2 face="sans-serif">Cheers</font>
<br><font size=2 face="sans-serif">Sebastian</font>
<br>
<br>
<br>
<br><font size=1 color=#5f5f5f face="sans-serif">Von: &nbsp; &nbsp; &nbsp;
&nbsp;</font><font size=1 face="sans-serif">Baz Walter &lt;bazwal@ftml.net&gt;</font>
<br><font size=1 color=#5f5f5f face="sans-serif">An: &nbsp; &nbsp; &nbsp;
&nbsp;</font><font size=1 face="sans-serif">pyqt@riverbankcomputing.com</font>
<br><font size=1 color=#5f5f5f face="sans-serif">Datum: &nbsp; &nbsp; &nbsp;
&nbsp;</font><font size=1 face="sans-serif">09.12.2016 20:51</font>
<br><font size=1 color=#5f5f5f face="sans-serif">Betreff: &nbsp; &nbsp;
&nbsp; &nbsp;</font><font size=1 face="sans-serif">Re: [PyQt] Multiple
Inheritance with PyQt5 on python 2</font>
<br><font size=1 color=#5f5f5f face="sans-serif">Gesendet von: &nbsp; &nbsp;
&nbsp; &nbsp;</font><font size=1 face="sans-serif">&quot;PyQt&quot;
&lt;pyqt-bounces@riverbankcomputing.com&gt;</font>
<br>
<hr noshade>
<br>
<br>
<br><tt><font size=2>On 09/12/16 12:22, Sebastian Eckweiler wrote:<br>
&gt; Hi there,<br>
&gt;<br>
&gt; I've just read through<br>
&gt; </font></tt><a href=http://pyqt.sourceforge.net/Docs/PyQt5/multiinheritance.html><tt><font \
size=2>http://pyqt.sourceforge.net/Docs/PyQt5/multiinheritance.html</font></tt></a><tt><font \
size=2>.<br> &gt; I'm using a self-built PyQt5 for Python 2 on Windows - and it seems
I<br>
&gt; can't get multiple inheritance to work:<br>
&gt;<br>
&gt; class SomeClass(object):<br>
&gt;<br>
&gt; &nbsp; &nbsp; def __init__(self, attr):<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; self.attr = attr<br>
&gt;<br>
&gt; class AnotherDialog(QtWidgets.QDialog, SomeClass):<br>
&gt;<br>
&gt; &nbsp; &nbsp; def __init__(self, parent=None):<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; #super(AnotherDialog, self).__init__(parent)
# tested this was<br>
&gt; well<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; QtWidgets.QDialog.__init__(self, parent)<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; SomeClass.__init__(self, 1)<br>
&gt;<br>
&gt;<br>
&gt; app = QtWidgets.QApplication([])<br>
&gt; dlg = AnotherDialog()<br>
&gt;<br>
&gt; This always results in<br>
&gt;<br>
&gt; TypeError: __init__() takes exactly 2 arguments (1 given)<br>
&gt;<br>
&gt; is there a workaround for this?<br>
<br>
The signature of SomeClass has one required positional argument. If you
<br>
use super, how is it going to get that argument if you don't pass it to
<br>
the AnotherDialog constructor?<br>
<br>
The docs you linked to explained that you should use keyword arguments.
<br>
So one way to resolve the issue would be like this:<br>
<br>
 &nbsp; &nbsp; class SomeClass(object):<br>
 &nbsp; &nbsp; &nbsp; &nbsp; def __init__(self, attr=1, **kwargs):<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; super(SomeClass, \
self).__init__(**kwargs)<br>  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.attr = \
attr<br> <br>
 &nbsp; &nbsp; class AnotherDialog(QtWidgets.QDialog, SomeClass):<br>
 &nbsp; &nbsp; &nbsp; &nbsp; def __init__(self, parent=None, **kwargs):<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; super(AnotherDialog, \
                self).__init__(parent,
**kwargs)<br>
<br>
Or, less flexibly, perhaps like this:<br>
<br>
 &nbsp; &nbsp; class SomeClass(object):<br>
 &nbsp; &nbsp; &nbsp; &nbsp; def __init__(self, attr):<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; super(SomeClass, self).__init__()<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.attr = attr<br>
<br>
 &nbsp; &nbsp; class AnotherDialog(QtWidgets.QDialog, SomeClass):<br>
 &nbsp; &nbsp; &nbsp; &nbsp; def __init__(self, parent=None):<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; super(AnotherDialog, \
self).__init__(parent, attr=1)<br>
<br>
Without super, put the mixin first:<br>
<br>
 &nbsp; &nbsp; class SomeClass(object):<br>
 &nbsp; &nbsp; &nbsp; &nbsp; def __init__(self, attr):<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.attr = attr<br>
<br>
 &nbsp; &nbsp; class AnotherDialog(SomeClass, QtWidgets.QDialog):<br>
 &nbsp; &nbsp; &nbsp; &nbsp; def __init__(self, parent=None):<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SomeClass.__init__(self, 1)<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; QtWidgets.QDialog.__init__(self,
parent)<br>
<br>
_______________________________________________<br>
PyQt mailing list &nbsp; &nbsp;PyQt@riverbankcomputing.com<br>
</font></tt><a href=https://www.riverbankcomputing.com/mailman/listinfo/pyqt><tt><font \
size=2>https://www.riverbankcomputing.com/mailman/listinfo/pyqt</font></tt></a> <br>
--=_alternative 003317D3C1258087_=--


[Attachment #3 (text/plain)]

_______________________________________________
PyQt mailing list    PyQt@riverbankcomputing.com
https://www.riverbankcomputing.com/mailman/listinfo/pyqt

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic