Popular recipes by Ryan Lie http://code.activestate.com/recipes/users/4171032/popular/2010-04-11T11:16:48-07:00ActiveState Code RecipesSimple 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>