星期四, 七月 06, 2006

介绍Python编程相关思想——动态函数重载(Dynamic Function Overloading)

All Things Pythonic
Dynamic Function Overloading
by Guido van Rossum
April 7, 2006
 
Summary
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. :-)
 
 
 
译:

Python编程思想

介绍Python编程相关思想
动态函数重载(Dynamic Function Overloading)

我已经向Python的subversion沙箱里提交了一个新的动态函数重载的实现。
尽管"(动态或运行时)重载函数[(dynamically, or run-time) overloaded functions]"这一术语受到各方批评,我还是要继续讲讲它。"(动态或运行时)重载函数"又可称为泛型函数(generic functions)、多重方法(multi-methods)或分派(dispatch),这些术语也都受到了批评。
大家可以在http://svn.python.org/view/sandbox/trunk/overload/上看到一个稍微优化过的实现。Phillip Eby曾说我的实现速度太慢,不过,我建议你们运行一下test_overloading.py,看看你们的情况怎么样。(这需要Python 2.5a 1版本,python.org刚刚发布了该版本。)
Phillip还建议我使用issubclass(),而不要依赖方法解析次序(Method Resolution Order,简称MRO)。他认为,MRO方法会导致虚假的准确性。我不知道是否会这样,因为对于单一派遣(single dispatch)而言,我的方法确实与类方法查找完全匹配。当然,这需要很多附加功能或潜在要求。例如,是否存在调用"next" 可用方法的机制?但是,算法没有清楚地生成下一个方法。另外,如果存在歧义,是否应该抛出异常或者调用缺省方法(或"next"方法,如果我们能够对它的定义达成一致的话)?
对于优化,我相信代码生成(code generation)可以让重载的函数和手写函数运行得一样快。请参见test_overloading.py中的accelerated。
这并不适用Python2.5。这是一个颇具争议的话题,所以应该到Python3000才来试验(也许可以作为Python2.5的第三方附加品)。
大约在一年前我曾经写过相关话题的blog,但是我却忘记有这么一回事了。与今天的情况相比,那时候的实现显得暗淡无光。看来,Plillip的提议还是帮了很大忙。
我希望最终将PyProtocols挤出市场。Plillip应该为我感到骄傲。

(原文链接网址:http://www.artima.com/weblogs/viewpost.jsp?thread=155514;Guido van Rossum的英文blog网址: http://www.artima.com/weblogs/index.jsp?blogger=guido

(翻译转自:http://blog.csdn.net/gvanrossum/category/217090.aspx

没有评论: