<div dir="ltr"><div class="gmail_extra">2013/5/15 Don Spaulding <span dir="ltr"><<a href="mailto:donspauldingii@gmail.com" target="_blank">donspauldingii@gmail.com</a>></span><br><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<div>Even if the only ordering change that was made was to magically give OrderedDict.__init__ its **kwargs in order, it would clean up these instances, which I initially referred to as literals.<br>
<br></div><div style="margin-left:40px"><span style="font-family:'courier new',monospace">foo = OrderedDict(<br></span></div><div style="margin-left:40px"><span style="font-family:'courier new',monospace">    b=1,<br>
</span></div>
<div><div style="margin-left:40px"><span style="font-family:'courier new',monospace">    a=2<br>)</span></div></div></blockquote></div><br>Since PEP3115, classes can <span style="color:rgb(0,0,0);line-height:17.8125px">__prepare__ a custom dict:</span></div>
<div class="gmail_extra"><span style="color:rgb(0,0,0);line-height:17.8125px"><br></span></div><div class="gmail_extra"><div class="gmail_extra"><font color="#000000" face="courier new, monospace"><span style="line-height:17.8125px">from collections import OrderedDict</span></font></div>
<div class="gmail_extra"><font color="#000000" face="courier new, monospace"><span style="line-height:17.8125px">class OrderedDictBuilder(type):</span></font></div><div class="gmail_extra"><font color="#000000" face="courier new, monospace"><span style="line-height:17.8125px">    @classmethod</span></font></div>
<div class="gmail_extra"><font color="#000000" face="courier new, monospace"><span style="line-height:17.8125px">    def __prepare__(metacls, name, bases):</span></font></div><div class="gmail_extra"><font color="#000000" face="courier new, monospace"><span style="line-height:17.8125px">        return OrderedDict()</span></font></div>
<div class="gmail_extra"><font face="courier new, monospace"><span style="line-height:17.8125px;color:rgb(0,0,0)">    def __new__(cls, name, bases, classdict):</span><br></font></div><div class="gmail_extra"><font color="#000000" face="courier new, monospace"><span style="line-height:17.8125px">        del classdict['__module__']  # ugh</span></font></div>
<div class="gmail_extra"><font color="#000000" face="courier new, monospace"><span style="line-height:17.8125px">        return classdict</span></font></div><div class="gmail_extra"><font color="#000000"><span style="line-height:17.8125px"><br>
</span></font></div><div class="gmail_extra" style><font color="#000000"><span style="line-height:17.8125px">Then we can (ab)use the Class syntax to preserve the order!</span></font></div><div class="gmail_extra"><font color="#000000"><span style="line-height:17.8125px"><br>
</span></font></div><div class="gmail_extra"><font color="#000000" face="courier new, monospace"><span style="line-height:17.8125px">class foo(metaclass=OrderedDictBuilder):</span></font></div><div class="gmail_extra"><font color="#000000" face="courier new, monospace"><span style="line-height:17.8125px">    b = 1</span></font></div>
<div class="gmail_extra"><font color="#000000" face="courier new, monospace"><span style="line-height:17.8125px">    a = 2</span></font></div><div class="gmail_extra"><font color="#000000" face="courier new, monospace"><span style="line-height:17.8125px"><br>
</span></font></div><div class="gmail_extra"><font color="#000000" face="courier new, monospace"><span style="line-height:17.8125px">assert repr(foo) == "OrderedDict([('b', 1), ('a', 2)])"</span><br>
</font></div><div class="gmail_extra" style><br></div></div><div class="gmail_extra" style>There is probably a way to get rid of the "metaclass=" part.</div><div class="gmail_extra" style>I'm not sure to like it, though.</div>
<div class="gmail_extra" style><br></div><div class="gmail_extra">-- <br>Amaury Forgeot d'Arc
</div></div>