Dynamic dispatch and inheritance in python

Posted by Bill Zimmerman on Stack Overflow See other posts from Stack Overflow or by Bill Zimmerman
Published on 2011-01-09T19:38:06Z Indexed on 2011/01/09 19:53 UTC
Read the original article Hit count: 264

Filed under:
|
|

Hi,

I'm trying to modify Guido's multimethod (dynamic dispatch code):

http://www.artima.com/weblogs/viewpost.jsp?thread=101605

to handle inheritance and possibly out of order arguments.

e.g. (inheritance problem)

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

Is there a better way than iteratively checking for the super() of each item until one is found?

e.g. (argument ordering problem) I was thinking of this from a collision detection standpoint.

e.g.

foo(Car(),Truck()) and
foo(Truck(), Car()) and

should both trigger

foo(Car,Truck) # Note: @multimethod(Truck,Car) will throw an exception if @multimethod(Car,Truck) was registered first?

I'm looking specifically for an 'elegant' solution. I know that I could just brute force my way through all the possibilities, but I'm trying to avoid that. I just wanted to get some input/ideas before sitting down and pounding out a solution.

Thanks

© Stack Overflow or respective owner

Related posts about python

Related posts about dynamic