Popular recipes tagged "overloading"http://code.activestate.com/recipes/tags/overloading/popular/2011-04-06T14:52:06-07:00ActiveState Code RecipesRoman numeral class with overloaded int methods (Python)
2011-04-06T14:52:06-07:00thom nealehttp://code.activestate.com/recipes/users/4176069/http://code.activestate.com/recipes/577640-roman-numeral-class-with-overloaded-int-methods/
<p style="color: grey">
Python
recipe 577640
by <a href="/recipes/users/4176069/">thom neale</a>
(<a href="/recipes/tags/homework/">homework</a>, <a href="/recipes/tags/int/">int</a>, <a href="/recipes/tags/overloading/">overloading</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/roman/">roman</a>).
</p>
<p>This Roman class is a subclass of int and supports the same methods int does, but any special methods that would normally return ints are return a new instance of Roman. You can use instances of this class in math expressions and a Roman instance will be returned, for example. </p>
<p>The class decorator used to achieve this was suggested by Alex Martelli '<a href="http://stackoverflow.com/questions/1242589/subclassing-int-to-attain-a-hex-representation/1243045#1243045">here</a> on stackoverlow.com.</p>
Simple Function Overloading with Decorator (Python)
2010-04-06T00:59:52-07:00Ryan Liehttp://code.activestate.com/recipes/users/4171032/http://code.activestate.com/recipes/577064-simple-function-overloading-with-decorator/
<p style="color: grey">
Python
recipe 577064
by <a href="/recipes/users/4171032/">Ryan Lie</a>
(<a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/function/">function</a>, <a href="/recipes/tags/overloading/">overloading</a>, <a href="/recipes/tags/python/">python</a>).
Revision 3.
</p>
<p>The recipe presents a simple decorator for function overloading in python. The @overloaded function searches for the first overloads that doesn't raise TypeError when called. Overloads are added to the overloads list by using the @func.overload_with decorator. The order of function definition determines which function gets tried first and once it founds a compatible function, it skips the rest of the overloads list.</p>
Type-Checking Function Overloading Decorator (Python)
2010-04-11T11:16:48-07:00Ryan Liehttp://code.activestate.com/recipes/users/4171032/http://code.activestate.com/recipes/577065-type-checking-function-overloading-decorator/
<p style="color: grey">
Python
recipe 577065
by <a href="/recipes/users/4171032/">Ryan Lie</a>
(<a href="/recipes/tags/checkng/">checkng</a>, <a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/function/">function</a>, <a href="/recipes/tags/overloading/">overloading</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/type/">type</a>).
Revision 2.
</p>
<p>The recipe presents a function overloading decorator in python that do type check. The type signature is marked with the @takes and @returns decorator, which causes the function to raise an InputParameterError exception if called with inappropriate arguments. The @overloaded function searches for the first overloads that doesn't raise TypeError or InputParameterError when called. Alternative overloads are added to the overloads list by using the @func.overload_with decorator. The order of function definition determines which function gets tried first and once it founds a compatible function, it skips the rest of the overloads list.</p>