Top-rated recipes tagged "class_decorator"http://code.activestate.com/recipes/tags/class_decorator/top/2013-12-10T02:36:25-08:00ActiveState Code Recipes Keyword Argument Injection with Python Decorators (Python)
2010-09-05T17:06:04-07:00Ahmet Emre Aladağhttp://code.activestate.com/recipes/users/4174877/http://code.activestate.com/recipes/577382-keyword-argument-injection-with-python-decorators/
<p style="color: grey">
Python
recipe 577382
by <a href="/recipes/users/4174877/">Ahmet Emre Aladağ</a>
(<a href="/recipes/tags/class/">class</a>, <a href="/recipes/tags/class_decorator/">class_decorator</a>, <a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/injection/">injection</a>, <a href="/recipes/tags/variable/">variable</a>).
Revision 2.
</p>
<p>In most of the object oriented codes we write, we need to set class attributes to the given argument values and this is a very line-consuming thing. To get over these redundant lines, I found a solution using decorators. </p>
Optional arguments decorator (Python)
2013-01-28T05:03:43-08:00Maxime H Lapointehttp://code.activestate.com/recipes/users/4185033/http://code.activestate.com/recipes/578435-optional-arguments-decorator/
<p style="color: grey">
Python
recipe 578435
by <a href="/recipes/users/4185033/">Maxime H Lapointe</a>
(<a href="/recipes/tags/class_decorator/">class_decorator</a>, <a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/decorators/">decorators</a>, <a href="/recipes/tags/python/">python</a>).
Revision 2.
</p>
<p>If you need your decorator to receive arguments, optional or not, then you should decorate it with the opt_arg_dec decorator to handle all the logic that this implies for you.</p>
Composition of classes instead of multiple inheritance (Python)
2011-04-16T03:40:19-07:00Ethan Furmanhttp://code.activestate.com/recipes/users/4177684/http://code.activestate.com/recipes/577658-composition-of-classes-instead-of-multiple-inherit/
<p style="color: grey">
Python
recipe 577658
by <a href="/recipes/users/4177684/">Ethan Furman</a>
(<a href="/recipes/tags/class_decorator/">class_decorator</a>, <a href="/recipes/tags/composition/">composition</a>, <a href="/recipes/tags/inheritance/">inheritance</a>, <a href="/recipes/tags/multiple_inheritance/">multiple_inheritance</a>).
</p>
<p>MI can be difficult and confusing, and if the base classes don't cooperate -- well, cooperative MI won't work.</p>
<p>One way around this is to use composition instead. This class decorator will combine the source classes with the target class, ensuring that no duplications occur (raises a TypeError if there are any).</p>
Total 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>
Bound Inner Classes (Python)
2013-12-10T02:36:25-08:00Larry Hastingshttp://code.activestate.com/recipes/users/2467657/http://code.activestate.com/recipes/577070-bound-inner-classes/
<p style="color: grey">
Python
recipe 577070
by <a href="/recipes/users/2467657/">Larry Hastings</a>
(<a href="/recipes/tags/class_decorator/">class_decorator</a>, <a href="/recipes/tags/descriptor/">descriptor</a>).
Revision 8.
</p>
<p>This recipe provides the class decorator <code>BoundInnerClass</code>. The decorator makes inner classes symmetric with method calls. Functions declared inside classes become "methods", and when you call them through an object they automatically get a reference to "self". The <code>BoundInnerClass</code> decorator makes this work for inner classes: an inner class decorated with <code>BoundInnerClass</code> gets a reference to that same (now "outer") object passed in automatically to the inner class's <code>__init__</code>.</p>
<p>The recipe works unchanged in Python 2.6 and 3.1, and is licensed using the Zlib license.</p>