[PySide] Is dual inheritance clearer?

Algis Kabaila akabaila at pcug.org.au
Fri Apr 5 01:35:54 CEST 2013


On Thursday 04 April 2013 17:09:50 Nik Melchior wrote:
> On Thu, Apr 04, 2013 at 08:10:29AM +0300, anatoly techtonik wrote:
> > On Thu, Apr 4, 2013 at 8:02 AM, Algis Kabaila <akabaila at pcug.org.au> 
wrote:
> > > **
> > > 
> > > Hi,
> > > 
> > > 
> > > 
> > > I believe that PySide (and PyQt for that matter) are widely used for
> > > GUI design. There are two main "orderly" ways to start a GUI
> > > program, after designing it in the Qt Creator:
> > > 
> > > 
> > > 
> > > (1)
> > > 
> > > With dual inheritance:
> > > 
> > > class MainWindow(QMainWindow, Ui_MainWindow):
> > > 
> > > def __init__(self, parent=None):
> > > 
> > > super(MainWindow, self).__init__(parent)
> > > 
> > > self.setupUi(self)
> > > 
> > > 
> > > 
> > > (2)
> > > 
> > > Without dual inheritance:
> > > 
> > > class MainWindow(QMainWindow):
> > > 
> > > def __init__(self, parent=None):
> > > 
> > > super(MainWindow, self).__init__(parent)
> > > 
> > > self.ui = Ui_MainWindow
> > > 
> > > self.ui.setupUi(Ui_MainWindow(),self)
> > > 
> > > 
> > > 
> > > There have been some horror stories about the evils of multiple
> > > inheritance. What is the preferred method, (1) or (2)?
> > 
> > I'd say - that depends on what Ui_MainWindow is, but in general case
> > (2) is> 
> > better, because:
> >  * self.ui defines a namespace to look for methods of UiMW object
> >  * explicit is better than implicit
> 
> I'm sorry this doesn't help resolve your confusion, but I prefer the
> opposite approach, due to:
> 
>   * Consistency with hand-written UI:  When I write simple applications
> or widgets that compose other widgets, the contained widgets are merely
> members; I do not relegate them to a separate namespace.  It's quite
> simple to do most layouts writing the code by hand, without designer. *
> IS-A vs. HAS-A: Your MainWindow class IS-A QMainWindow as much as it
> IS-A UI_MainWindow.  It cannot be instantiated without both parent
> classes, and it does not make sense to consider replacing the self.ui
> object with anything polymorphicly related to the Ui_MainWindow class. 
> I think the only reason the UI_MainWindow is a separate class is due to
> the convenience of placing auto-generated code into its own file.
> 
> It's really little more than a style choice, but I think the consistency
> argument will be important as you start to write more and more
> Qt/PySide classes.  Unless your hand-coded UIs contain a self.ui
> namespace, you will confuse yourself trying to remember if a particular
> QObject's member functions should access its widgets as, say,
> self.labelWidget or self.ui.labelWidget.
> 
> --
> Nik Melchior
> Sr. Roboticist       | 412-379-4500 x147
> Seegrid Corporation  | www.seegrid.com
> 216 Park West Drive, Pittsburgh, PA 15275
> Email Confidentiality Notice
> 
> The information contained in this transmission is confidential,
> proprietary or privileged and may be subject to protection under the
> law. This message is intended for the sole use of the individual or
> entity to whom it's addressed. If you are not the intended recipient,
> you are notified that any use, distribution or copying of the message
> is strictly prohibited and may subject you to criminal or civil
> penalties. If you received this transmission in error, please contact
> the sender immediately by replying to this email and delete the
> material from any computer.

Thank you for your thoughtful and detail analysis of the question.  Your 
observation that   "the UI_MainWindow is a separate class is due to
 the convenience of placing auto-generated code into its own file." 
Well IMHO tutorials for the "newbies" (not just in programming GUI with 
PySide, but also in other endeavours) always should aim at simplicity and 
consistency.  

So I read your  well considered contribution as +1 for dual inheritance.  
Thank you for taking time to present an analysis of the problem. 

Thanks too to Anatoly, who gave the -1 answer.  All views are appreciated. 
Of course, it is a style problem.  I just had a reason to look at PEP-8.  
I think one of the reasons why Python is such a great success is that the 
author of the original, Guido Van Rossum,  is able so clearly to elucidate 
the need not to follow blindly the rules:

"-- sometimes the style guide just doesn't apply. When in doubt, use your 
best judgment. Look at other examples and decide what looks best. And 
don't hesitate to ask! "

And say thank you!
Al.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/pyside/attachments/20130405/e7cb0faf/attachment.html>


More information about the PySide mailing list