<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Support for Cooperative Multi-inheritance — PyQt 5.1.1 Reference Guide</title> <link rel="stylesheet" href="_static/default.css" type="text/css" /> <link rel="stylesheet" href="_static/pygments.css" type="text/css" /> <script type="text/javascript"> var DOCUMENTATION_OPTIONS = { URL_ROOT: '', VERSION: '5.1.1', COLLAPSE_INDEX: false, FILE_SUFFIX: '.html', HAS_SOURCE: true }; </script> <script type="text/javascript" src="_static/jquery.js"></script> <script type="text/javascript" src="_static/underscore.js"></script> <script type="text/javascript" src="_static/doctools.js"></script> <link rel="shortcut icon" href="_static/logo_tn.ico"/> <link rel="top" title="PyQt 5.1.1 Reference Guide" href="index.html" /> <link rel="next" title="Things to be Aware Of" href="gotchas.html" /> <link rel="prev" title="Integrating Python and QML" href="qml.html" /> </head> <body> <div class="related"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="genindex.html" title="General Index" accesskey="I">index</a></li> <li class="right" > <a href="py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="gotchas.html" title="Things to be Aware Of" accesskey="N">next</a> |</li> <li class="right" > <a href="qml.html" title="Integrating Python and QML" accesskey="P">previous</a> |</li> <li><a href="index.html">PyQt 5.1.1 Reference Guide</a> »</li> </ul> </div> <div class="document"> <div class="documentwrapper"> <div class="bodywrapper"> <div class="body"> <div class="section" id="support-for-cooperative-multi-inheritance"> <span id="ref-cooperative-multiinheritance"></span><h1>Support for Cooperative Multi-inheritance<a class="headerlink" href="#support-for-cooperative-multi-inheritance" title="Permalink to this headline">ΒΆ</a></h1> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last">This section is not about sub-classing from more that one Qt class.</p> </div> <p>Cooperative multi-inheritance is a technique for implementing classes that inherit multiple super-classes - typically a main super-class and one or more mixin classes that add additional behaviour. It makes it easy to add new mixins at a later date to further extend the behavior, without needing to change either the implementation of the class or any existing code that creates an instance of the class.</p> <p>The technique requires that all the super-class’s <tt class="docutils literal"><span class="pre">__init__</span></tt> methods follow the same pattern in the way that they handle unrecognised keyword arguments and use <tt class="docutils literal"><span class="pre">super()</span></tt> to invoke their own super-class’s <tt class="docutils literal"><span class="pre">__init__</span></tt> methods.</p> <p>PyQt5’s classes follow this pattern.</p> <p>See Raymond Hettinger’s <a class="reference external" href="http://rhettinger.wordpress.com/2011/05/26/super-considered-super/">Python’s super() considered super!</a> blog post for some more background on the subject.</p> <p>As an example, let’s say we have a class that represents a person, and that a person has a name. The following might be an initial implementation:</p> <div class="highlight-python"><pre>class Person(QObject): def __init__(self, name, parent=None) QObject.__init__(self, parent) self.name = name</pre> </div> <p>An instance would normally be created in one of the following ways:</p> <div class="highlight-python"><div class="highlight"><pre><span class="n">person</span> <span class="o">=</span> <span class="n">Person</span><span class="p">(</span><span class="s">"Joe"</span><span class="p">)</span> <span class="n">person</span> <span class="o">=</span> <span class="n">Person</span><span class="p">(</span><span class="s">"Joe"</span><span class="p">,</span> <span class="n">some_parent</span><span class="p">)</span> </pre></div> </div> <p>This approach has some limitations:</p> <ul class="simple"> <li>Only a sub-set of the <a class="reference internal" href="api/qobject.html#PyQt5.QtCore.QObject" title="PyQt5.QtCore.QObject"><tt class="xref py py-class docutils literal"><span class="pre">QObject</span></tt></a> API is exposed. For example you cannot set the value of a Qt property or connect a signal by passing appropriate keyword arguments to <tt class="docutils literal"><span class="pre">Person.__init__</span></tt>.</li> <li>Adding another class to <tt class="docutils literal"><span class="pre">Person</span></tt>‘s list of super-classes means that its <tt class="docutils literal"><span class="pre">__init__</span></tt> implementation needs to be changed. If the new mixin takes non-optional arguments then every call to create a <tt class="docutils literal"><span class="pre">Person</span></tt> instance will need changing.</li> </ul> <p>Consider this alternative implementation:</p> <div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">Person</span><span class="p">(</span><span class="n">QObject</span><span class="p">):</span> <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="o">**</span><span class="n">kwds</span><span class="p">):</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">__init__</span><span class="p">(</span><span class="o">**</span><span class="n">kwds</span><span class="p">)</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="n">name</span> </pre></div> </div> <p>The difference is that we only handle arguments that are used by the <tt class="docutils literal"><span class="pre">Person</span></tt> class itself and we punt all the other arguments to the super-classes by calling <tt class="docutils literal"><span class="pre">super()</span></tt>.</p> <p>With this implementation an instance would normally created in one of the following ways:</p> <div class="highlight-python"><div class="highlight"><pre><span class="n">person</span> <span class="o">=</span> <span class="n">Person</span><span class="p">(</span><span class="s">"Joe"</span><span class="p">)</span> <span class="n">person</span> <span class="o">=</span> <span class="n">Person</span><span class="p">(</span><span class="s">"Joe"</span><span class="p">,</span> <span class="n">parent</span><span class="o">=</span><span class="n">some_parent</span><span class="p">)</span> </pre></div> </div> <p>Here the difference is that we are using keyword arguments to specify any arguments that are not handled by the <tt class="docutils literal"><span class="pre">Person</span></tt> class itself. Note that we could use keyword arguments for all arguments - whether or not you do so is down to personal choice.</p> <p>The limitations of the first implementation no longer apply. For example, without any further changes we can also do this:</p> <div class="highlight-python"><div class="highlight"><pre><span class="n">person</span> <span class="o">=</span> <span class="n">Person</span><span class="p">(</span><span class="s">"Joe"</span><span class="p">,</span> <span class="n">destroyed</span><span class="o">=</span><span class="n">some_callable</span><span class="p">)</span> </pre></div> </div> <p>Let’s say we now want to extend the behaviour of the <tt class="docutils literal"><span class="pre">Person</span></tt> class by adding a mixin that handles a person’s age. The implementation of the mixin would be as follows:</p> <div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">Age</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span> <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">age</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="o">**</span><span class="n">kwds</span><span class="p">):</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">__init__</span><span class="p">(</span><span class="o">**</span><span class="n">kwds</span><span class="p">)</span> <span class="bp">self</span><span class="o">.</span><span class="n">age</span> <span class="o">=</span> <span class="n">age</span> </pre></div> </div> <p>This follows a similar pattern to our <tt class="docutils literal"><span class="pre">Person</span></tt> implementation, but notice that we have provided the <tt class="docutils literal"><span class="pre">age</span></tt> argument with a default value.</p> <p>The following is our new <tt class="docutils literal"><span class="pre">Person</span></tt> implementation:</p> <div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">Person</span><span class="p">(</span><span class="n">QObject</span><span class="p">,</span> <span class="n">Age</span><span class="p">):</span> <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="o">**</span><span class="n">kwds</span><span class="p">):</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">__init__</span><span class="p">(</span><span class="o">**</span><span class="n">kwds</span><span class="p">)</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="n">name</span> </pre></div> </div> <p>The only change we have had to make is to add <tt class="docutils literal"><span class="pre">Age</span></tt> to <tt class="docutils literal"><span class="pre">Person</span></tt>‘s list of super-classes. More importantly we do not need to change any call to create a <tt class="docutils literal"><span class="pre">Person</span></tt> instance.</p> <p>If we do want to create a <tt class="docutils literal"><span class="pre">Person</span></tt> instance with a non-default age then we simply pass it as a keyword argument as follows:</p> <div class="highlight-python"><div class="highlight"><pre><span class="n">person</span> <span class="o">=</span> <span class="n">Person</span><span class="p">(</span><span class="s">"Joe"</span><span class="p">,</span> <span class="n">age</span><span class="o">=</span><span class="mi">38</span><span class="p">)</span> </pre></div> </div> <p>This technique increases the use of keyword arguments - while this means a bit more typing, it significantly increases the readability of application code.</p> </div> </div> </div> </div> <div class="sphinxsidebar"> <div class="sphinxsidebarwrapper"> <p class="logo"><a href="index.html"> <img class="logo" src="_static/logo.png" alt="Logo"/> </a></p> <h4>Previous topic</h4> <p class="topless"><a href="qml.html" title="previous chapter">Integrating Python and QML</a></p> <h4>Next topic</h4> <p class="topless"><a href="gotchas.html" title="next chapter">Things to be Aware Of</a></p> <div id="searchbox" style="display: none"> <h3>Quick search</h3> <form class="search" action="search.html" method="get"> <input type="text" name="q" /> <input type="submit" value="Go" /> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> <p class="searchtip" style="font-size: 90%"> Enter search terms or a module, class or function name. </p> </div> <script type="text/javascript">$('#searchbox').show(0);</script> </div> </div> <div class="clearer"></div> </div> <div class="related"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="genindex.html" title="General Index" >index</a></li> <li class="right" > <a href="py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="gotchas.html" title="Things to be Aware Of" >next</a> |</li> <li class="right" > <a href="qml.html" title="Integrating Python and QML" >previous</a> |</li> <li><a href="index.html">PyQt 5.1.1 Reference Guide</a> »</li> </ul> </div> <div class="footer"> © Copyright 2013 Riverbank Computing Limited. Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3. </div> </body> </html>