<?xml version="1.0" encoding="UTF-8" ?>
<rdf:RDF xmlns="http://usefulinc.com/ns/doap#" xmlns:foaf="http://xmlns.com/foaf/0.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"><Project><name>strait</name>
<shortdesc>Simple Traits for Python</shortdesc>
<description>&lt;/pre&gt;&lt;?xml version="1.0" encoding="utf-8" ?&gt;
&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
&lt;html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;
&lt;meta name="generator" content="Docutils 0.4.1: http://docutils.sourceforge.net/" /&gt;
&lt;title&gt;A simple implementation of traits for Python&lt;/title&gt;
&lt;meta name="author" content="Michele Simionato" /&gt;
&lt;meta name="date" content="2009-01-10" /&gt;
&lt;style type="text/css"&gt;

.highlight  { background: #f8f8f8; }
.highlight .c { color: #408080; font-style: italic } /* Comment */
.highlight .err { border: 1px solid #FF0000 } /* Error */
.highlight .k { color: #008000; font-weight: bold } /* Keyword */
.highlight .o { color: #666666 } /* Operator */
.highlight .cm { color: #408080; font-style: italic } /* Comment.Multiline */
.highlight .cp { color: #BC7A00 } /* Comment.Preproc */
.highlight .c1 { color: #408080; font-style: italic } /* Comment.Single */
.highlight .cs { color: #408080; font-style: italic } /* Comment.Special */
.highlight .gd { color: #A00000 } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gr { color: #FF0000 } /* Generic.Error */
.highlight .gh { color: #000080; font-weight: bold } /* Generic.Heading */
.highlight .gi { color: #00A000 } /* Generic.Inserted */
.highlight .go { color: #808080 } /* Generic.Output */
.highlight .gp { color: #000080; font-weight: bold } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #800080; font-weight: bold } /* Generic.Subheading */
.highlight .gt { color: #0040D0 } /* Generic.Traceback */
.highlight .kc { color: #008000; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008000; font-weight: bold } /* Keyword.Declaration */
.highlight .kp { color: #008000 } /* Keyword.Pseudo */
.highlight .kr { color: #008000; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #008000; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #666666 } /* Literal.Number */
.highlight .s { color: #BA2121 } /* Literal.String */
.highlight .na { color: #7D9029 } /* Name.Attribute */
.highlight .nb { color: #008000 } /* Name.Builtin */
.highlight .nc { color: #0000FF; font-weight: bold } /* Name.Class */
.highlight .no { color: #880000 } /* Name.Constant */
.highlight .nd { color: #AA22FF } /* Name.Decorator */
.highlight .ni { color: #999999; font-weight: bold } /* Name.Entity */
.highlight .ne { color: #D2413A; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0000FF } /* Name.Function */
.highlight .nl { color: #A0A000 } /* Name.Label */
.highlight .nn { color: #0000FF; font-weight: bold } /* Name.Namespace */
.highlight .nt { color: #008000; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #19177C } /* Name.Variable */
.highlight .ow { color: #AA22FF; font-weight: bold } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mf { color: #666666 } /* Literal.Number.Float */
.highlight .mh { color: #666666 } /* Literal.Number.Hex */
.highlight .mi { color: #666666 } /* Literal.Number.Integer */
.highlight .mo { color: #666666 } /* Literal.Number.Oct */
.highlight .sb { color: #BA2121 } /* Literal.String.Backtick */
.highlight .sc { color: #BA2121 } /* Literal.String.Char */
.highlight .sd { color: #BA2121; font-style: italic } /* Literal.String.Doc */
.highlight .s2 { color: #BA2121 } /* Literal.String.Double */
.highlight .se { color: #BB6622; font-weight: bold } /* Literal.String.Escape */
.highlight .sh { color: #BA2121 } /* Literal.String.Heredoc */
.highlight .si { color: #BB6688; font-weight: bold } /* Literal.String.Interpol */
.highlight .sx { color: #008000 } /* Literal.String.Other */
.highlight .sr { color: #BB6688 } /* Literal.String.Regex */
.highlight .s1 { color: #BA2121 } /* Literal.String.Single */
.highlight .ss { color: #19177C } /* Literal.String.Symbol */
.highlight .bp { color: #008000 } /* Name.Builtin.Pseudo */
.highlight .vc { color: #19177C } /* Name.Variable.Class */
.highlight .vg { color: #19177C } /* Name.Variable.Global */
.highlight .vi { color: #19177C } /* Name.Variable.Instance */
.highlight .il { color: #666666 } /* Literal.Number.Integer.Long */

&lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div class="document" id="a-simple-implementation-of-traits-for-python"&gt;
&lt;h1 class="title"&gt;A simple implementation of traits for Python&lt;/h1&gt;
&lt;table class="docinfo" frame="void" rules="none"&gt;
&lt;col class="docinfo-name" /&gt;
&lt;col class="docinfo-content" /&gt;
&lt;tbody valign="top"&gt;
&lt;tr&gt;&lt;th class="docinfo-name"&gt;Author:&lt;/th&gt;
&lt;td&gt;Michele Simionato&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;th class="docinfo-name"&gt;Date:&lt;/th&gt;
&lt;td&gt;2009-01-10&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;th class="docinfo-name"&gt;Version:&lt;/th&gt;
&lt;td&gt;0.5.1&lt;/td&gt;&lt;/tr&gt;
&lt;tr class="field"&gt;&lt;th class="docinfo-name"&gt;Download:&lt;/th&gt;&lt;td class="field-body"&gt;&lt;a class="reference" href="http://pypi.python.org/pypi/strait"&gt;http://pypi.python.org/pypi/strait&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr class="field"&gt;&lt;th class="docinfo-name"&gt;Licence:&lt;/th&gt;&lt;td class="field-body"&gt;BSD&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;div class="abstract topic"&gt;
&lt;p class="topic-title first"&gt;Abstract&lt;/p&gt;
&lt;p&gt;&lt;em&gt;I provide a simple implementation of
traits as units of composable behavior for Python. I
argue that traits are better than multiple inheritance.
Implementing frameworks based on traits is left as an exercise
for the reader.&lt;/em&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h1&gt;&lt;a id="motivation" name="motivation"&gt;Motivation&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;Multiple inheritance is a hotly debated topic.
The supporters of multiple inheritance
claim that it makes code shorter and easier
to read, whereas the opposers claim that is makes
code more coupled and more difficult to understand.  I have
spent some time in the past facing the intricacies of &lt;a class="reference" href="MRO"&gt;multiple
inheritance in Python&lt;/a&gt; and I was one of its supporters once; however,
since then I have worked with frameworks making
large use of multiple inheritance (I mean Zope 2) and nowadays I am in
the number of the people who oppose it.  Therefore I
am interested in alternatives.&lt;/p&gt;
&lt;p&gt;In recent years, the approach
of &lt;a class="reference" href="http://www.iam.unibe.ch/~scg/Research/Traits/"&gt;traits&lt;/a&gt; has gained some traction in a few circles and I have
decided to write a library to implement traits in Python, for
experimentation purposes. The library is meant for framework builders,
people who are thinking about writing a framework based on multiple
inheritance - typically via the common mixin approach - but
are not convinced that this is the best solution and would like to try
an alternative. This library is also for authors of mixin-bases frameworks
which are unsatisfied and would like to convert their
framework to traits.&lt;/p&gt;
&lt;p&gt;Are traits a better solution than multiple inheritance and mixins?  In
theory I think so, otherwise I would not have written this library, but
in practice (as always) things may be different. It may well be
that using traits or using mixins does not make a big
difference in practice
and that the change of paradigm is not worth the effort; or the
opposite may be true. The only way to know is to try, to build
software based on traits and to see how it scale &lt;em&gt;in the large&lt;/em&gt;.
In the small, more or less any approach works fine: it is only
by programming in the large that you can see the differences.&lt;/p&gt;
&lt;p&gt;This is
the reason why I am releasing this library with a liberal licence, so
that people can try it out and see how it works. The library is meant
to play well (when possible) with pre-existing frameworks.
As an example, I will show
here how you could rewrite Tkinter classes to use traits instead of mixins. Of
course, I am not advocating rewriting Tkinter: it would be silly
and pointless; but it may have sense (or
not) to rewrite your own framework using traits, perhaps a framework
which is used in house but has not been released yet.&lt;/p&gt;
&lt;p&gt;I am not the only one to have
implemented traits for Python; after finishing my implementation
I made a little research and discovered a few implementations. Then
I have also discovered the &lt;a class="reference" href="https://svn.enthought.com/enthought/wiki/Traits"&gt;Enthought Traits&lt;/a&gt; framework, which however
seems to use the name to intend something completely
different (i.e. a sort of type checking). My implementation has no
dependencies, is short and I am committed
to keep it short even in the future, according to
the principle of &lt;a class="reference" href="http://www.artima.com/weblogs/viewpost.jsp?thread=236286"&gt;less is more&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;There is also an hidden agenda behind this module: to popularize some
advanced features of the Python object model which are little
known. The &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;strait&lt;/span&gt;&lt;/tt&gt; module is actually a tribute to the
metaprogramming capabilities of Python: such features are usually
associated to languages with a strong academic tradition - Smalltalk,
Scheme, Lisp - but actually the Python object model is no less
powerful.  For instance, changing the object system from a multiple
inheritance one to a trait-based one,
can be done &lt;em&gt;within&lt;/em&gt; the fundamental object system. The reason is that
the features that Guido used to implement the object system (special
method hooks, descriptors, metaclasses) are there, available to the
end user to build her own object system.&lt;/p&gt;
&lt;p&gt;Such features are usually little used in the Python community, for
many good reasons: most people feel that the object system is good
enough and that there is no reason to change it; moreover there is
a strong opposition to change the language, because Python programmers
believe in uniformity and in using common idioms; finally, it is
difficult for an application programmer to find a domain where these
features are useful. An exception is the domain of the Object Relation
Mappers, whereas the Python language is often stretched to mimic the
SQL language, a famous example of this tendency being &lt;a class="reference" href="http://www.sqlalchemy.org/"&gt;SQLAlchemy&lt;/a&gt;).
Still, I have never seen a perversion of the object model as big as
the one implemented in the &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;strait&lt;/span&gt;&lt;/tt&gt; module, so I wanted to be the
first one to perform that kind of abuse ;)&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h1&gt;&lt;a id="what-are-traits" name="what-are-traits"&gt;What are traits?&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;The word &lt;em&gt;traits&lt;/em&gt; has many meanings; I will refer to it in the sense
of the paper &lt;a class="reference" href="http://www.iam.unibe.ch/%7Escg/Archive/Papers/Scha03aTraits.pdf"&gt;Traits - Composable Units of Behavior&lt;/a&gt; which implements
them in Squeak/Smalltalk. The paper appeared in 2003, but most of the
ideas underlying traits have been floating around for at least 30
years. There is also a trait implementation for &lt;a class="reference" href="http://www.cs.utah.edu/plt/publications/aplas06-fff.pdf"&gt;PLT Scheme&lt;/a&gt; which is
somewhat close in spirit (if not in practice) to what I am advocating here.
The library you are reading about is by no means intended as a porting
of the Smalltalk library: I am just stealing some of the ideas from
that paper to implement a Pythonic alternative to mixins which, for
lack of a better name, I have decided to call traits. I feel no
obligation whatsoever to be consistent with the Smalltalk library. In
doing so, I am following a long tradition, since a lot of languages
use the name &lt;em&gt;traits&lt;/em&gt; to mean something completely different from the
Smalltalk meaning. For instance the languages Fortress and Scala use
the name &lt;em&gt;trait&lt;/em&gt; but with a different meaning (Scala traits are very
close to multiple inheritance).
For me a trait is a bunch of methods and attributes with the following
properties:&lt;/p&gt;
&lt;ol class="arabic simple"&gt;
&lt;li&gt;the methods/attributes in a trait belong logically together;&lt;/li&gt;
&lt;li&gt;if a trait enhances a class, then all subclasses are enhanced too;&lt;/li&gt;
&lt;li&gt;if a trait has methods in common with the class, then the
methods defined in the class have the precedence;&lt;/li&gt;
&lt;li&gt;the trait order is not important, i.e. enhancing a class
first with trait T1 and then with trait T2 or viceversa is the same;&lt;/li&gt;
&lt;li&gt;if traits T1 and T2 have names in common, enhancing a class both
with T1 and T2 raises an error;&lt;/li&gt;
&lt;li&gt;if a trait has methods in common with the base class, then the
trait methods have the precedence;&lt;/li&gt;
&lt;li&gt;a class can be seen both as a composition of traits and as an homogeneous
entity.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Properties from 4 to 7 are the distinguishing properties of traits
with respect to multiple inheritance and mixins.  In particular,
because of 4 and 5, all the complications with the Method Resolution
Order disappear and the overriding is never implicit.  Property 6 is
mostly unusual: typically in Python the base class has the precedence
over mixin classes.  Property 7 should be intended in the sense that a
trait implementation must provide introspection facilities to make
seemless the transition between classes viewed as atomic entities and
as composed entities.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h1&gt;&lt;a id="a-hands-on-example" name="a-hands-on-example"&gt;A hands-on example&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;Let me begin by showing how you could rewrite a
Tkinter class to use traits instead of mixins. Consider the
&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;Tkinter.Widget&lt;/span&gt;&lt;/tt&gt; class, which is derived by the base class
&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;BaseWidget&lt;/span&gt;&lt;/tt&gt; and the mixin classes
&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;Tkinter.Grid&lt;/span&gt;&lt;/tt&gt;, &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;Tkinter.Pack&lt;/span&gt;&lt;/tt&gt; and &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;Tkinter.Place&lt;/span&gt;&lt;/tt&gt;: I want to
rewrite it by using traits. The &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;strait&lt;/span&gt;&lt;/tt&gt; module
provides a factory function named &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;include&lt;/span&gt;&lt;/tt&gt; that does the job.
It is enough to replace the multiple inheritance syntax:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
class Widget(BaseWidget, Grid, Pack, Place):
     pass
&lt;/pre&gt;
&lt;p&gt;with the following syntax:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
class Widget(BaseWidget):
    __metaclass__ = include(Pack, Place, Grid)
&lt;/pre&gt;
&lt;p&gt;I said that the conversion from mixins to traits was easy: but actually
I lied since if you try to execute the code I just wrote you will
get an &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;OverridingError&lt;/span&gt;&lt;/tt&gt;:&lt;/p&gt;
&lt;blockquote&gt;
&lt;pre class="doctest-block"&gt;
&amp;gt;&amp;gt;&amp;gt; from Tkinter import *
&amp;gt;&amp;gt;&amp;gt; class Widget(BaseWidget):
...     __metaclass__ = include(Pack, Place, Grid)
Traceback (most recent call last):
  ...
OverridingError: Pack overrides names in Place: {info, config, configure,
slaves, forget}
&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;p&gt;The reason for the error is clear: both &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;Pack&lt;/span&gt;&lt;/tt&gt; and &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;Place&lt;/span&gt;&lt;/tt&gt; provide
methods called &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;{info,&lt;/span&gt; &lt;span class="pre"&gt;config,&lt;/span&gt; &lt;span class="pre"&gt;configure,&lt;/span&gt; &lt;span class="pre"&gt;slaves,&lt;/span&gt; &lt;span class="pre"&gt;forget}&lt;/span&gt;&lt;/tt&gt;
and the traits implementation cannot figure out
which ones to use.  This is a feature, since it forces you to be
explicit. In this case, if we want to be consistent with
multiple inheritance rules, we want the methods coming from
the first class (i.e. &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;Pack&lt;/span&gt;&lt;/tt&gt;) to take precedence. That can be
implemented by including directly those methods in the class namespace
and relying on rule 3:&lt;/p&gt;
&lt;div class="codeblock python"&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;TOSWidget&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;BaseWidget&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;__metaclass__&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;include&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Pack&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Place&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Grid&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;info&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Pack&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;info&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;im_func&lt;/span&gt;
    &lt;span class="n"&gt;config&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Pack&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;im_func&lt;/span&gt;
    &lt;span class="n"&gt;configure&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Pack&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;configure&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;im_func&lt;/span&gt;
    &lt;span class="n"&gt;slaves&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Pack&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;slaves&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;im_func&lt;/span&gt;
    &lt;span class="n"&gt;forget&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Pack&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;forget&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;im_func&lt;/span&gt;
    &lt;span class="n"&gt;propagate&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Pack&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;propagate&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;im_func&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;/div&gt;
&lt;p&gt;Notice that we had to specify the &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;propagate&lt;/span&gt;&lt;/tt&gt; method too, since
it is a common method between &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;Pack&lt;/span&gt;&lt;/tt&gt; and &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;Grid&lt;/span&gt;&lt;/tt&gt;.&lt;/p&gt;
&lt;p&gt;You can check that the TOSWidget class works, for instance by defining a
label widget as follows (remember that &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;TOSWidget&lt;/span&gt;&lt;/tt&gt; inherits its signature
from  &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;BaseWidget&lt;/span&gt;&lt;/tt&gt;):&lt;/p&gt;
&lt;blockquote&gt;
&lt;pre class="doctest-block"&gt;
&amp;gt;&amp;gt;&amp;gt; label = TOSWidget(master=None, widgetName='label',
...                   cnf=dict(text=&amp;quot;hello&amp;quot;))
&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;p&gt;You may visualize the widget by calling the &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;.pack&lt;/span&gt;&lt;/tt&gt; method:&lt;/p&gt;
&lt;blockquote&gt;
&lt;pre class="doctest-block"&gt;
&amp;gt;&amp;gt;&amp;gt; label.pack()
&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;p&gt;This should open a small window with the message &amp;quot;hello&amp;quot; inside it.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h1&gt;&lt;a id="a-few-caveats-and-warnings" name="a-few-caveats-and-warnings"&gt;A few caveats and warnings&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;First of all, let me notice that, in spite of apparency, &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;include&lt;/span&gt;&lt;/tt&gt;
does not return a metaclass. Insted, it returns a class factory
function with signature &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;name,&lt;/span&gt; &lt;span class="pre"&gt;bases,&lt;/span&gt; &lt;span class="pre"&gt;dic&lt;/span&gt;&lt;/tt&gt;:&lt;/p&gt;
&lt;pre class="doctest-block"&gt;
&amp;gt;&amp;gt;&amp;gt; print include(Pack, Place, Grid)
&amp;lt;function include_Pack_Place_Grid at 0x...&amp;gt;
&lt;/pre&gt;
&lt;p&gt;This function will create the class by using a suitable
metaclass:&lt;/p&gt;
&lt;pre class="doctest-block"&gt;
&amp;gt;&amp;gt;&amp;gt; type(TOSWidget)
&amp;lt;class 'strait.MetaTOS'&amp;gt;
&lt;/pre&gt;
&lt;p&gt;In simple cases the metaclass will be &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;MetaTOS&lt;/span&gt;&lt;/tt&gt;, the main class
of the trait object system, but in general it can be a different
one not inheriting from &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;MetaTOS&lt;/span&gt;&lt;/tt&gt;. The exact rules followed by
&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;include&lt;/span&gt;&lt;/tt&gt; to determine the right class will be discussed later.&lt;/p&gt;
&lt;p&gt;Here I want to remark that according to rule 6 traits take the precedence
over the base class attributes. Consider the following example:&lt;/p&gt;
&lt;blockquote&gt;
&lt;pre class="doctest-block"&gt;
&amp;gt;&amp;gt;&amp;gt; class Base(object):
...     a = 1
&lt;/pre&gt;
&lt;pre class="doctest-block"&gt;
&amp;gt;&amp;gt;&amp;gt; class ATrait(object):
...     a = 2
&lt;/pre&gt;
&lt;pre class="doctest-block"&gt;
&amp;gt;&amp;gt;&amp;gt; class Class(Base):
...     __metaclass__ = include(ATrait)
&lt;/pre&gt;
&lt;pre class="doctest-block"&gt;
&amp;gt;&amp;gt;&amp;gt; Class.a
2
&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;p&gt;In regular multiple inheritance you would do the same by including
&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;ATrait&lt;/span&gt;&lt;/tt&gt; &lt;em&gt;before&lt;/em&gt; &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;Base&lt;/span&gt;&lt;/tt&gt;, i.e.&lt;/p&gt;
&lt;blockquote&gt;
&lt;pre class="doctest-block"&gt;
&amp;gt;&amp;gt;&amp;gt; type('Class', (ATrait, Base), {}).a
2
&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;p&gt;You should take care to not mix-up the order, otherwise you will get a
different result:&lt;/p&gt;
&lt;blockquote&gt;
&lt;pre class="doctest-block"&gt;
&amp;gt;&amp;gt;&amp;gt; type('Class', (Base, ATrait), {}).a
1
&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;p&gt;Therefore replacing mixin classes with traits can break your code if
you rely on the order. Be careful!&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h1&gt;&lt;a id="the-trait-object-system" name="the-trait-object-system"&gt;The Trait Object System&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;The goal of the &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;strait&lt;/span&gt;&lt;/tt&gt; module it to modify the standard
Python object model, turning it into a Trait Object System (TOS for short):
TOS classes behave differently from regular
classes. In particular TOS classes do not support multiple inheritance.
If you try to multiple inherit from a TOS
class and another class you will get a &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;TypeError&lt;/span&gt;&lt;/tt&gt;:&lt;/p&gt;
&lt;blockquote&gt;
&lt;pre class="doctest-block"&gt;
&amp;gt;&amp;gt;&amp;gt; class M:
...     &amp;quot;An empty class&amp;quot;
...
&amp;gt;&amp;gt;&amp;gt; class Widget2(TOSWidget, M):
...     pass
...
Traceback (most recent call last):
  ...
TypeError: Multiple inheritance of bases (&amp;lt;class '__main__.TOSWidget'&amp;gt;, &amp;lt;class
__main__.M at 0x...&amp;gt;) is forbidden for TOS classes
&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;p&gt;This behavior is intentional: with this restriction you can simulate
an ideal world in which Python did not support multiple
inheritance. Suppose you want to claim that supporting multiple
inheritance was a mistake and that Python would have been better off
without it (which is the position I tend to have nowadays): how can
you prove that claim? Simply by writing code that does not use
multiple inheritance and it is clearer and more mantainable that code
using multiple inheritance.&lt;/p&gt;
&lt;p&gt;I am releasing this trait implementation hoping you will help me to
prove (or possibly disprove) the point.  You may see traits as a
restricted form of multiple inheritance without name clashes,
without the complications of the method resolution, and with a
limited cooperation between methods.
Moreover the present implementation is slightly less dynamic
than usual inheritance.&lt;/p&gt;
&lt;p&gt;A nice property of inheritance is that if you have a class &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;C&lt;/span&gt;&lt;/tt&gt; inheriting
from class &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;M&lt;/span&gt;&lt;/tt&gt; and you change a method in &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;M&lt;/span&gt;&lt;/tt&gt; at runtime, after
&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;C&lt;/span&gt;&lt;/tt&gt; has been created and instantiated, automagically all instances
of &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;C&lt;/span&gt;&lt;/tt&gt; gets the new version of the method, which is pretty useful
for debugging purposes. This feature is lost in the trait implementation
provided here. Actually, in a previous version, my trait implementation
was fully dynamic and if you changed the mixin the instances would be
changed too. However, I never used that feature in practice, and
it was complicating the implementation and slowing doing the
attribute access, so I removed it.&lt;/p&gt;
&lt;p&gt;I think these are acceptable restrictions since they give back
in return many advantages in terms of simplicity: for instance,
&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;super&lt;/span&gt;&lt;/tt&gt; becomes trivial, since each class has a single superclass,
whereas we all know that the &lt;a class="reference" href="http://www.artima.com/weblogs/viewpost.jsp?thread=236275"&gt;current super in Python&lt;/a&gt; is very far
from trivial.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h1&gt;&lt;a id="the-magic-of-include" name="the-magic-of-include"&gt;The magic of &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;include&lt;/span&gt;&lt;/tt&gt;&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;Since the fundamental properties of TOS classes must be preserved under
inheritance (i.e. the son of a TOS class must be a TOS class)
the implementation necessarily requires metaclasses. As of now,
the only fundamental property of a TOS class is that multiple
inheritance is forbidden, so usually (&lt;em&gt;but not always&lt;/em&gt;) TOS
classes are instances of the metaclass &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;MetaTOS&lt;/span&gt;&lt;/tt&gt;
which implements a single inheritance check.
If you build your TOS hierarchy starting from pre-existing classes,
you should be aware of how &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;include&lt;/span&gt;&lt;/tt&gt; determines the metaclass:
if your base class was an old-style
class or a plain new style class (i.e. a direct instance of the
&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;type&lt;/span&gt;&lt;/tt&gt; metaclass), them &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;include&lt;/span&gt;&lt;/tt&gt; will change it to &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;MetaTOS&lt;/span&gt;&lt;/tt&gt;:&lt;/p&gt;
&lt;pre class="doctest-block"&gt;
&amp;gt;&amp;gt;&amp;gt; type(TOSWidget)
&amp;lt;class 'strait.MetaTOS'&amp;gt;
&lt;/pre&gt;
&lt;p&gt;In general you may need to build your Trait Based Framework
on top of pre-existing classes possessing a nontrivial metaclass, for
instance Zope classes; in that case &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;include&lt;/span&gt;&lt;/tt&gt; is smart
enough to figure out the right metaclass to use. Here is an example:&lt;/p&gt;
&lt;div class="codeblock python"&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;AddGreetings&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;type&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="s"&gt;&amp;quot;A metaclass adding a &amp;#39;greetings&amp;#39; attribute for exemplification purposes&amp;quot;&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__new__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mcl&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;bases&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;dic&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;dic&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;&amp;#39;greetings&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;&amp;#39;hello!&amp;#39;&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;super&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;AddGreetings&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;mcl&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;__new__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mcl&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;bases&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;dic&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;/div&gt;
&lt;div class="codeblock python"&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;WidgetWithGreetings&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;BaseWidget&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;object&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;__metaclass__&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;AddGreetings&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;/div&gt;
&lt;div class="codeblock python"&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;PackWidget&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;WidgetWithGreetings&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;__metaclass__&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;include&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Pack&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;/div&gt;
&lt;p&gt;&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;include&lt;/span&gt;&lt;/tt&gt; automatically generates the right metaclass as
a subclass of &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;AddGreetings&lt;/span&gt;&lt;/tt&gt;:&lt;/p&gt;
&lt;blockquote&gt;
&lt;pre class="doctest-block"&gt;
&amp;gt;&amp;gt;&amp;gt; print type(PackWidget).__mro__
(&amp;lt;class 'strait._TOSAddGreetings'&amp;gt;, &amp;lt;class '__main__.AddGreetings'&amp;gt;, &amp;lt;type
'type'&amp;gt;, &amp;lt;type 'object'&amp;gt;)
&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;p&gt;Incidentally, since TOS
classes are guaranteed to be in a straight hierarchy, &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;include&lt;/span&gt;&lt;/tt&gt; is able
to neatly avoid the dreaded &lt;a class="reference" href="http://code.activestate.com/recipes/204197/"&gt;metaclass conflict&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The important point is that &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;_TOSAddGreetings&lt;/span&gt;&lt;/tt&gt; provides the same features of
&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;MetaTOS&lt;/span&gt;&lt;/tt&gt;, even if it is not a subclass of it; on the
other hand, &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;_TOSMetaAddGreetings&lt;/span&gt;&lt;/tt&gt; is a subclass of &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;AddGreetings&lt;/span&gt;&lt;/tt&gt;
which calls &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;AddGreetings.__new__&lt;/span&gt;&lt;/tt&gt;, so the features provided by
&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;AddGreetings&lt;/span&gt;&lt;/tt&gt; are not lost either; in this example you may check
that the greetings attribute is correctly set:&lt;/p&gt;
&lt;blockquote&gt;
&lt;pre class="doctest-block"&gt;
&amp;gt;&amp;gt;&amp;gt; PackWidget.greetings
'hello!'
&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;p&gt;The name of the generated metaclass
is automatically generated from the name of the base
metaclass; moreover, a register of the generated metaclasses
is kept, so that metaclasses are reused if possible.
If you want to understand the details, you are welcome
to give a look at the implementation, which is pretty short
and simple, compared to the general recipe to remove
the metaclass conflict in a true multiple inheritance situation.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h1&gt;&lt;a id="cooperative-traits" name="cooperative-traits"&gt;Cooperative traits&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;At first sight, the Trait Object System lacks an important feature of
multiple inheritance as implemented in the ordinary Python object system,
i.e. cooperative methods.  Consider for instance the following
classes:&lt;/p&gt;
&lt;div class="codeblock python"&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;LogOnInitMI&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;object&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;kw&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;print&lt;/span&gt; &lt;span class="s"&gt;&amp;#39;Initializing &lt;/span&gt;&lt;span class="si"&gt;%s&lt;/span&gt;&lt;span class="s"&gt;&amp;#39;&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="bp"&gt;self&lt;/span&gt;
        &lt;span class="nb"&gt;super&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;LogOnInitMI&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;kw&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;/div&gt;
&lt;div class="codeblock python"&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;RegisterOnInitMI&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;object&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;register&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;kw&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;print&lt;/span&gt; &lt;span class="s"&gt;&amp;#39;Registering &lt;/span&gt;&lt;span class="si"&gt;%s&lt;/span&gt;&lt;span class="s"&gt;&amp;#39;&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="bp"&gt;self&lt;/span&gt;
        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;register&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nb"&gt;super&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;RegisterOnInitMI&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;kw&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;/div&gt;
&lt;p&gt;In multiple inheritance &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;LogOnInitMI&lt;/span&gt;&lt;/tt&gt; can be mixed with other
classes, giving to the children the ability to log on initialization;
the same is true for &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;RegisterOnInitMI&lt;/span&gt;&lt;/tt&gt;, which gives to its children
the ability to populate a registry of instances. The important feature
of the multiple inheritance system is that &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;LogOnInitMI&lt;/span&gt;&lt;/tt&gt; and
&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;RegisterOnInitMI&lt;/span&gt;&lt;/tt&gt; play well together: if you inherits from
both of them, you get both features:&lt;/p&gt;
&lt;div class="codeblock python"&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;C_MI&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;LogOnInitMI&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;RegisterOnInitMI&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;pass&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;/div&gt;
&lt;pre class="doctest-block"&gt;
&amp;gt;&amp;gt;&amp;gt; c = C_MI()
Initializing &amp;lt;__main__.C_MI object at 0x...&amp;gt;
Registering &amp;lt;__main__.C_MI object at 0x...&amp;gt;
&lt;/pre&gt;
&lt;p&gt;You cannot get the same behaviour if you use the trait object system
naively:&lt;/p&gt;
&lt;pre class="doctest-block"&gt;
&amp;gt;&amp;gt;&amp;gt; class C_MI(object):
...      __metaclass__ = include(LogOnInitMI, RegisterOnInitMI)
...
Traceback (most recent call last):
  ...
OverridingError: LogOnInitMI overrides names in RegisterOnInitMI: {__init__}
&lt;/pre&gt;
&lt;p&gt;This is a feature, of course, since the trait object system is designed
to avoid name clashes. However, the situation is worse than that:
even if you try to mixin a single class you will run into trouble&lt;/p&gt;
&lt;pre class="doctest-block"&gt;
&amp;gt;&amp;gt;&amp;gt; class C_MI(object):
...      __metaclass__ = include(LogOnInitMI)
&lt;/pre&gt;
&lt;pre class="doctest-block"&gt;
&amp;gt;&amp;gt;&amp;gt; c = C_MI()
Traceback (most recent call last):
  ...
TypeError: super(type, obj): obj must be an instance or subtype of type
&lt;/pre&gt;
&lt;p&gt;What's happening here? The situation is clear if you notice that the
&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;super&lt;/span&gt;&lt;/tt&gt; call is actually a call of kind &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;super(LogOnInitMI,&lt;/span&gt; &lt;span class="pre"&gt;c)&lt;/span&gt;&lt;/tt&gt;
where &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;c&lt;/span&gt;&lt;/tt&gt; is an instance of &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;C&lt;/span&gt;&lt;/tt&gt;, which is not a
subclass of &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;LogOnInitMI&lt;/span&gt;&lt;/tt&gt;. That explains the
error message, but does not explain how to solve the issue. It seems
that method cooperation using &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;super&lt;/span&gt;&lt;/tt&gt; is impossible for TOS
classes.&lt;/p&gt;
&lt;p&gt;Actually this is not the case: single inheritance cooperation
is possible and it is enough as we will show in a
minute. But for the moment let me notice that I do not think
that cooperative methods are necessarily a good idea. They are
fragile and cause all of your classes to be strictly coupled. My usual
advice if that you should not use a design based on method
cooperation if you can avoid it.
Having said that, there are situations (very rare) where you
really want method cooperation. The &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;strait&lt;/span&gt;&lt;/tt&gt; module provide
support for those situations via the &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;__super&lt;/span&gt;&lt;/tt&gt; attribute.&lt;/p&gt;
&lt;p&gt;Let me explain how it works. When you mix-in a trait &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;T&lt;/span&gt;&lt;/tt&gt; into a
class &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;C&lt;/span&gt;&lt;/tt&gt;, &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;include&lt;/span&gt;&lt;/tt&gt; adds an attribute &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;_T__super&lt;/span&gt;&lt;/tt&gt; to &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;C&lt;/span&gt;&lt;/tt&gt;,
which is a &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;super&lt;/span&gt;&lt;/tt&gt; object that dispatches to the attributes of the
superclass of &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;C&lt;/span&gt;&lt;/tt&gt;. The important thing to keep in mind is that there
is a well defined superclass, since the trait object system uses
single inheritance only. Since the hierarchy is straight, the
cooperation mechanism is much simpler to understand than in multiple
inheritance. Here is an example. First of all, let me rewrite
&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;LogOnInit&lt;/span&gt;&lt;/tt&gt; and &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;RegisterOnInit&lt;/span&gt;&lt;/tt&gt; to use &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;__super&lt;/span&gt;&lt;/tt&gt; instead of
&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;super&lt;/span&gt;&lt;/tt&gt;:&lt;/p&gt;
&lt;div class="codeblock python"&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;LogOnInit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;object&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;kw&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;print&lt;/span&gt; &lt;span class="s"&gt;&amp;#39;Initializing &lt;/span&gt;&lt;span class="si"&gt;%s&lt;/span&gt;&lt;span class="s"&gt;&amp;#39;&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="bp"&gt;self&lt;/span&gt;
        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;__super&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;kw&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;/div&gt;
&lt;div class="codeblock python"&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;RegisterOnInit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;object&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;register&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;kw&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;print&lt;/span&gt; &lt;span class="s"&gt;&amp;#39;Registering &lt;/span&gt;&lt;span class="si"&gt;%s&lt;/span&gt;&lt;span class="s"&gt;&amp;#39;&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="bp"&gt;self&lt;/span&gt;
        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;register&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;__super&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;kw&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;/div&gt;
&lt;p&gt;Now you can include the &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;RegisterOnInit&lt;/span&gt;&lt;/tt&gt; functionality as follows:&lt;/p&gt;
&lt;div class="codeblock python"&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;C_Register&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;object&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;__metaclass__&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;include&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;RegisterOnInit&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;/div&gt;
&lt;pre class="doctest-block"&gt;
&amp;gt;&amp;gt;&amp;gt; _ = C_Register()
Registering &amp;lt;__main__.C_Register object at 0x...&amp;gt;
&lt;/pre&gt;
&lt;p&gt;Everything works because &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;include&lt;/span&gt;&lt;/tt&gt; has added the right attribute:&lt;/p&gt;
&lt;pre class="doctest-block"&gt;
&amp;gt;&amp;gt;&amp;gt; C_Register._RegisterOnInit__super
&amp;lt;super: &amp;lt;class 'C_Register'&amp;gt;, &amp;lt;C_Register object&amp;gt;&amp;gt;
&lt;/pre&gt;
&lt;p&gt;Moreover, you can also include the &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;LogOnInit&lt;/span&gt;&lt;/tt&gt; functionality:&lt;/p&gt;
&lt;div class="codeblock python"&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;C_LogAndRegister&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;C_Register&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;__metaclass__&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;include&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;LogOnInit&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;/div&gt;
&lt;pre class="doctest-block"&gt;
&amp;gt;&amp;gt;&amp;gt; _ = C_LogAndRegister()
Initializing &amp;lt;__main__.C_LogAndRegister object at 0x...&amp;gt;
Registering &amp;lt;__main__.C_LogAndRegister object at 0x...&amp;gt;
&lt;/pre&gt;
&lt;p&gt;As you see, the cooperation mechanism works just fine. I will call
&lt;em&gt;cooperative trait&lt;/em&gt; a class intended for inclusion in other classes
and making use of the &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;__super&lt;/span&gt;&lt;/tt&gt; trick. A class using the
regular &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;super&lt;/span&gt;&lt;/tt&gt; directly cannot be used as a cooperative trait, since it
must satisfy inheritance constraints, nevertherless it is easy enough to
convert it to use &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;__super&lt;/span&gt;&lt;/tt&gt;. After all, the &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;strait&lt;/span&gt;&lt;/tt&gt; module is
intended for framework writers, so it assumes you can change the
source code of your framework if you want. On the other hand, if
are trying to re-use a mixin class coming from a third party
framework and using &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;super&lt;/span&gt;&lt;/tt&gt;, you will have to rewrite the
parts of it. That is unfortunate, but I cannot perform miracles.&lt;/p&gt;
&lt;p&gt;You may see &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;__super&lt;/span&gt;&lt;/tt&gt; as a clever hack to use
&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;super&lt;/span&gt;&lt;/tt&gt; indirectly. Notice that since the hierarchy is straight,
there is room for optimization at the core language
level. The &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;__super&lt;/span&gt;&lt;/tt&gt; trick as implemented in pure Python leverages
on the name mangling mechanism, and follows closely the famous
&lt;a class="reference" href="http://www.python.org/download/releases/2.2.3/descrintro/#metaclass_examples"&gt;autosuper recipe&lt;/a&gt;, with some improvement. Anyway,
if you have two traits with the same
name, you will run into trouble. To solve this and to have a nicer
syntax, one would need more support from the language, but the
&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;__super&lt;/span&gt;&lt;/tt&gt; trick is good enough for a prototype and
has the serious advantage of working right now for current Python.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h1&gt;&lt;a id="cooperation-at-the-metaclass-level" name="cooperation-at-the-metaclass-level"&gt;Cooperation at the metaclass level&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;In my experience, the cases where you need method cooperation
in multiple inheritance situations are exceedingly rare,
unless you are a language implementor or a designer of
very advanced frameworks. In such a realm you have a need for
cooperative methods; it is not a pressing need, in the sense that
you can always live without them, but they are a nice feature to have if you
care about elegance and extensibility. For instance, as P. J. Eby
points it out in this &lt;a class="reference" href="http://www.gossamer-threads.com/lists/python/dev/673833"&gt;thread on python-dev&lt;/a&gt;:&lt;/p&gt;
&lt;p&gt;&lt;em&gt;A major use case for co-operative super() is in the implementation of
metaclasses.  The __init__ and __new__ signatures are fixed, multiple
inheritance is possible, and co-operativeness is a must (as the base
class methods&lt;/em&gt; must &lt;em&gt;be called).  I'm hard-pressed to think of a
metaclass constructor or initializer that I've written in the last
half-decade or more where I didn't use super() to make it
co-operative. That, IMO, is a compelling use case even if there were
not a single other example of the need for super.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;I have always felt the same. So, even if I have been unhappy with multiple
inheritance for years, I could never dismiss it entirely
because of the concern for this use case. It is only after discovering
cooperative traits that I felt the approach powerful enough
to replace multiple inheritance without losing anything I cared about.&lt;/p&gt;
&lt;p&gt;Multiple inheritance at the metaclass level comes out here and
again when you are wearing the language implementor hat. For instance,
if you try to implement an object system based on traits, you will have to do
so at the metaclass level and there method cooperation has its place.
In particular, if you look at the source code of the &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;strait&lt;/span&gt;&lt;/tt&gt; module -
which is around 100 lines, a tribute to the power of Python -
you will see that the &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;MetaTOS&lt;/span&gt;&lt;/tt&gt; metaclass is implemented
as a cooperative trait, so that it can be mixed-in with other metaclasses,
in the case you are interoperating with a framework with a non-trivial
meta object protocol. This is performed internally by &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;include&lt;/span&gt;&lt;/tt&gt;.&lt;/p&gt;
&lt;p&gt;Metaclass cooperation is there to make the life of the users
easier. Suppose one of you, users of the &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;strait&lt;/span&gt;&lt;/tt&gt; module, wants to
enhance the &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;include&lt;/span&gt;&lt;/tt&gt; mechanism using another a metaclass coming for
a third party framework and therefore not inheriting from &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;MetaTOS&lt;/span&gt;&lt;/tt&gt;:&lt;/p&gt;
&lt;div class="codeblock python"&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ThirdPartyMeta&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;type&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__new__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mcl&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;bases&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;dic&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;print&lt;/span&gt; &lt;span class="s"&gt;&amp;#39;Using ThirdPartyMeta to create &lt;/span&gt;&lt;span class="si"&gt;%s&lt;/span&gt;&lt;span class="s"&gt;&amp;#39;&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;super&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ThirdPartyMeta&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;mcl&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;__new__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mcl&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;bases&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;dic&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;/div&gt;
&lt;p&gt;The way to go is simple. First, you should mix-in &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;MetaTOS&lt;/span&gt;&lt;/tt&gt; in the
third party class:&lt;/p&gt;
&lt;div class="codeblock python"&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;EnhancedMetaTOS&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ThirdPartyMeta&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;__metaclass__&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;include&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;MetaTOS&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;/div&gt;
&lt;p&gt;Then, you can define your own enhanced &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;include&lt;/span&gt;&lt;/tt&gt; as follows:&lt;/p&gt;
&lt;div class="codeblock python"&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;enhanced_include&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;traits&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;include&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;MetaTOS&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;EnhancedMetaTOS&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;traits&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;/div&gt;
&lt;p&gt;In simple cases using directly &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;ThirdPartyMeta&lt;/span&gt;&lt;/tt&gt; may work, but I strongly
recommend to replace the call to super with &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;__super&lt;/span&gt;&lt;/tt&gt; even in
&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;ThirdPartyMeta&lt;/span&gt;&lt;/tt&gt; to make the cooperation robust.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h1&gt;&lt;a id="discussion-of-some-design-decisions-and-future-work" name="discussion-of-some-design-decisions-and-future-work"&gt;Discussion of some design decisions and future work&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;The decision of having TOS classes which are not instances of
&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;MetaTOS&lt;/span&gt;&lt;/tt&gt;
required some thought. That was my original idea in version 0.1 of
&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;strait&lt;/span&gt;&lt;/tt&gt;; however in version 0.2 I wanted to see what would happen
if I made all TOS classes instances of &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;MetaTOS&lt;/span&gt;&lt;/tt&gt;.
That implied that if
your original class had a nontrivial metaclass, then the TOS class had
to inherit both from the original metaclass &lt;em&gt;and&lt;/em&gt; &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;MetaTOS&lt;/span&gt;&lt;/tt&gt;,
i.e. multiple inheritance and cooperation of methods was required at
the metaclass level.&lt;/p&gt;
&lt;p&gt;I did not like it, since I was arguing that
you can do everything without multiple inheritance; moreover using
multiple inheritance at the metaclass level
meant that one had to solve the metaclass conflict in a general
way. I did so, by using my own cookbook recipe, and all my tests
passed.&lt;/p&gt;
&lt;p&gt;Neverthess, at the end, in version 0.3 I decided to go back to the
original design. The metaclass conflict recipe is too complex, and I
see it as a code smell - &lt;em&gt;if the implementation is hard to explain,
it's a bad idea&lt;/em&gt; - just another indication that multiple inheritance
is bad. In the original design it is possible to add the features of
&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;MetaTOS&lt;/span&gt;&lt;/tt&gt; to the original metaclass by subclassing it with &lt;em&gt;single&lt;/em&gt;
inheritance and thus avoiding the conflict.&lt;/p&gt;
&lt;p&gt;The price to pay is that now a TOS class is no more an instance of
&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;MetaTOS&lt;/span&gt;&lt;/tt&gt;, but this is a non-issue: the important
thing is that TOS classes perform the dispatch on their traits as
&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;MetaTOS&lt;/span&gt;&lt;/tt&gt; would dictate.  Moreover, starting from
Python 2.6, thanks to &lt;a class="reference" href="http://www.python.org/dev/peps/pep-3119/"&gt;Abstract Base Classes&lt;/a&gt;, you may satisfy the
&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;isinstance(obj,&lt;/span&gt; &lt;span class="pre"&gt;cls)&lt;/span&gt;&lt;/tt&gt; check even if &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;obj&lt;/span&gt;&lt;/tt&gt; is not an instance of
&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;cls&lt;/span&gt;&lt;/tt&gt;, by registering a suitable base class (similarly for
&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;issubclass&lt;/span&gt;&lt;/tt&gt;). In our situation, that means that it is enough to
register &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;MetaTOS&lt;/span&gt;&lt;/tt&gt; as base class of the original
metaclass.&lt;/p&gt;
&lt;p&gt;Version 0.4 was much more complex that the current version (still
short, it was under 300 lines of pure Python), since it had the more
ambitious goal of solving the namespace pollution problem. I have
discussed the issue &lt;a class="reference" href="http://www.artima.com/weblogs/viewpost.jsp?thread=246341"&gt;elsewhere&lt;/a&gt;: if you keep injecting methods into a
class (both directly or via inheritance) you may end up having
hundreds of methods flattened at the same level.&lt;/p&gt;
&lt;p&gt;A picture is worth a
thousand words, so have a look at the &lt;a class="reference" href="http://www.phyast.pitt.edu/~micheles/python/plone-hierarchy.png"&gt;PloneSite hierarchy&lt;/a&gt; if you
want to understand the horror I wanted to avoid with traits (the
picture shows the number of nonspecial attributes defined per class in
square brackets): in the Plone Site hierarchy there are 38 classes, 88
overridden names, 42 special names, 648 non-special attributes and
methods. It is a nighmare.&lt;/p&gt;
&lt;p&gt;Originally I wanted to prevent this kind
of abuse, but that made my implementation more complex, whereas
my main goal was to keep the implementation simple. As a consequence
this version assume the prosaic attitude that you cannot stop
programmers from bad design anyway, so if they want to go the Zope
way they can.&lt;/p&gt;
&lt;p&gt;In previous versions I did provide some syntactic sugar for &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;include&lt;/span&gt;&lt;/tt&gt;
so that it was possible to write something like the following
(using a trick discussed &lt;a class="reference" href="http://www.ibm.com/developerworks/linux/library/l-pymeta3.html"&gt;here&lt;/a&gt;):&lt;/p&gt;
&lt;pre class="literal-block"&gt;
class C(Base):
   include(Trait1, Trait2)
&lt;/pre&gt;
&lt;p&gt;In version 0.5 I decided to remove this feature. Now the plumbing
(i.e. the &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;__metaclass__&lt;/span&gt;&lt;/tt&gt; hook) is exposed to the user, some magic
has been removed and it is easier for the user to write her own
&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;include&lt;/span&gt;&lt;/tt&gt; factory if she wants to.&lt;/p&gt;
&lt;p&gt;Where to go from here? For the moment, I have no clear idea about the
future. The Smalltalk implementation of traits provides method
renaming out of the box. The Python implementation has no facilities
in this sense. In the future I may decide to give some support for
renaming, or I may not. At the present you can just rename your
methods by hand.  Also, in the future I may decide to add some kind of
adaptation mechanism or I may not: after all the primary goal of this
implementation is semplicity and I don't want to clutter it with too
many features.&lt;/p&gt;
&lt;p&gt;I am very open to feedback and criticism: I am releasing this module
with the hope that it will be used in real life situations to gather
experience with the traits concept. Clearly I am not proposing that
Python should remove multiple inheritance in favor of traits:
considerations of backward compatibily would kill the proposal right
from the start. I am just looking for a few adventurous volunteers
wanting to experiment with traits; if the experiment goes well, and
people start using (multiple) inheritance less than they do now, I
will be happy.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h1&gt;&lt;a id="trivia" name="trivia"&gt;Trivia&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;strait&lt;/span&gt;&lt;/tt&gt; officially stands for Simple Trait object system, however
the name is also a pun on the world &amp;quot;straight&amp;quot;, since the difference
between multiple inheritance hierarchies and TOS hierarchies is that
TOS hierarchies are straight. Moreover, nobody will stop you from
thinking that the &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;s&lt;/span&gt;&lt;/tt&gt; also stands for Simionato ;)&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="footer"&gt;
&lt;hr class="footer" /&gt;
&lt;a class="reference" href="docs.rst"&gt;View document source&lt;/a&gt;.
Generated on: 2009-01-10 10:52 UTC.
Generated by &lt;a class="reference" href="http://docutils.sourceforge.net/"&gt;Docutils&lt;/a&gt; from &lt;a class="reference" href="http://docutils.sourceforge.net/rst.html"&gt;reStructuredText&lt;/a&gt; source.

&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;
&lt;pre&gt;</description>
<homepage rdf:resource="http://pypi.python.org/pypi/strait" />
<maintainer><foaf:Person><foaf:name>Michele Simionato</foaf:name>
<foaf:mbox_sha1sum>ab893da5395b555f05aea49f8d6fd4378261e15f</foaf:mbox_sha1sum></foaf:Person></maintainer>
<release><Version><revision>0.5.1</revision></Version></release>
</Project></rdf:RDF>