Sponsored Link •
I've checked an implementation of dynamic function overloading into Python's subversion sandbox.
I'm going to stick to the term "(dynamically, or run-time) overloaded functions" for now, despite criticism of this term; the alternatives "generic functions", "multi-methods" and "dispatch" have been (rightly) criticized as well.
A slightly optimized implementation is here: http://svn.python.org/view/sandbox/trunk/overload/
Phillip Eby has suggested that my implementation is too slow; but I recommend that you run test_overloading.py and see for yourself. (This requires the Python 2.5a1 release that was just released from python.org.)
Phillip also suggested that I use issubclass() instead of relying on the MRO (method resolution order); he believes the MRO approach can cause false precision. I'm not sure I believe that, since for single dispatch my approach actually matches the class method lookup approach exactly.
There are tons of additional features or potential requirements. For example, should there be a mechanism to invoke the "next" applicable method? But the algorithm doesn't clearly produce a next method. Also, in the case of ambiguity, should an exception be raised or the default method be invoked? (Or perhaps the "next" method, if we can agree on what it is?)
In terms of optimization, I believe that a little bit of code generation could make overloaded functions nearly as fast as hand-coded functions; see 'accelerated' in test_overloading.py.
And no, this isn't for Python 2.5; it's a highly contagious but also highly controversial thing that should be tried out in Python 3000 (or perhaps as a 3rd party add-on for 2.5) first.
I totally forgot that I blogged about this earlier, over a year ago. My implementation then was pathetic compared to today's! Phillip's ideas have helped a lot.
My hope is to eventually drive PyProtocols out of the market. Phillip should be so proud of me. :-)
|Guido van Rossum is the creator of Python, one of the major programming languages on and off the web. The Python community refers to him as the BDFL (Benevolent Dictator For Life), a title straight from a Monty Python skit. He moved from the Netherlands to the USA in 1995, where he met his wife. Until July 2003 they lived in the northern Virginia suburbs of Washington, DC with their son Orlijn, who was born in 2001. They then moved to Silicon Valley where Guido now works for Google (spending 50% of his time on Python!).|