[Tutor] Generic functions

anil maran anilmrn at yahoo.com
Sat Aug 5 14:58:27 CEST 2006


i have a hard time understanding, why these are a better idea than if then else structures . Here is the page, that is not clear

thanks a lot 

First Try at Generic FunctionsWiki:First Try at G… Edit Goto Help. First Try at Generic Functions ... However,  I have long meant to use his generic functions, as I think they seem pretty ...
blog.ianbicking.org/first-try-at-generic-functions.html - 41k - Cached - Similar pages Python Buzz Forum - First Try at Generic FunctionsFirst Try at Generic Functions, Posted: Nov 1, 2005 2:42 AM ... Original Post:  First Try at Generic Functions Feed Title: Ian Bicking ...
www.artima.com/forums/flat.jsp?forum=122&thread=134755 - 17k -Cached - Similar pages

 """Why are generic functions better than this technique?"""
 
   Your technique will not affect any modules that used from jsonify import jsonify to obtain the function, because they won't see your replacement version.
   Your technique degrades performance linearly with the number of types added, while generic functions use a dispatch tree that uses hash tables, binary search, or other lookup techniques as appropriate. The dispatch tree is automatically balanced to test the most discriminating criteria first. So, adding a new type in the JSON example has virtually no effect on performance of the jsonify() function, versus one extra function-call of overhead added by your approach for each new type.
   Your technique can only add tests at the highest precedence level, which means that if there's any overlap in conditions between what you are adding, and what exists, you will need to code a more complex "if" rule, and you will need to know what other existing rules overlap. (Which means that only linear extension is possible, and that some tests will be repeated unnecessarily.)
   Your technique cannot take advantage of any overlap in the conditions being tested.  For example, the various isinstance() calls in your version repeatedly walk the object's class' __mro__ to see if it contains the target classes.  The generic function, however, will do this walk once, no matter how many types are registered, and then call the correct function.
   Your code is buggy; it should return jsonify_orig(obj).  :)
 In other words, generic functions are far superior to monkeypatching for creating an extensible operation, which is why the Python standard library uses a simple form of them for things like copying and pickling. However, the RuleDispatch generic function implementation supports automating the registration process and lookup tables for simple functions (using @dispatch.on) and allows for building dispatch trees on arbitrary computations with full generic functions (using @dispatch.generic).
 


 		
---------------------------------
Groups are talking. We´re listening. Check out the handy changes to Yahoo! Groups. 
 		
---------------------------------
Yahoo! Messenger with Voice. Make PC-to-Phone Calls to the US (and 30+ countries) for 2¢/min or less.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20060805/0c9a26a3/attachment.htm 


More information about the Tutor mailing list