我试图修改圭多的多重方法(动态调度代码):动态分配和继承在Python
http://www.artima.com/weblogs/viewpost.jsp?thread=101605
处理继承和可能失灵的论点。
例如(继承问题)
class A(object):
pass
class B(A):
pass
@multimethod(A,A)
def foo(arg1,arg2):
print 'works'
foo(A(),A()) #works
foo(A(),B()) #fails
是否有更好的方法比迭代检查每个项目的super()直到找到一个?
例如(参数排序问题) 我从碰撞检测的角度考虑了这个问题。
例如
foo(Car(),Truck()) and
foo(Truck(), Car()) and
都应该引起
foo(Car,Truck) # Note: @multimethod(Truck,Car) will throw an exception if @multimethod(Car,Truck) was registered first?
我一个 '优雅' 的解决方案专找。我知道我可以通过所有可能性蛮横的力量,但我试图避免这种情况。我只想在坐下并敲出解决方案之前获得一些意见和建议。
感谢
您可能想看看PEAK规则,这是python中可以处理继承的另一个通用调度实现。 http://pypi.python.org/pypi/PEAK-Rules – albertov 2011-01-09 19:53:30