Popular Python recipes tagged "ordering"http://code.activestate.com/recipes/langs/python/tags/ordering/2008-10-28T11:54:42-07:00ActiveState Code RecipesTotal Ordering: Class Decorator for Filling in Rich Comparison Methods When Only One is Implemented (Python)
2008-10-28T11:54:42-07:00Michael Foordhttp://code.activestate.com/recipes/users/2183852/http://code.activestate.com/recipes/576529-total-ordering-class-decorator-for-filling-in-rich/
<p style="color: grey">
Python
recipe 576529
by <a href="/recipes/users/2183852/">Michael Foord</a>
(<a href="/recipes/tags/class_decorator/">class_decorator</a>, <a href="/recipes/tags/comparison/">comparison</a>, <a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/ordering/">ordering</a>, <a href="/recipes/tags/sorting/">sorting</a>).
Revision 5.
</p>
<p><code>total_ordering</code> and <code>force_total_ordering</code> are class decorators for
Python 2.6 & Python 3.</p>
<p>They provides <em>all</em> the rich comparison methods on a class by defining <em>any</em>
one of '__lt__', '__gt__', '__le__', '__ge__'.</p>
<p><code>total_ordering</code> fills in all unimplemented rich comparison methods, assuming
at least one is implemented. <code>__lt__</code> is taken as the base comparison method
on which the others are built, but if that is not available it will be
constructed from the first one found.</p>
<p><code>force_total_ordering</code> does the same, but having taken a comparison method as
the base it fills in <em>all</em> the others - this overwrites additional comparison
methods that may be implemented, guaranteeing consistent comparison semantics.</p>