Multiple dispatch again

Anders J. Munch andersjm at dancontrol.dk
Thu Jan 2 16:34:53 CET 2003


"David Mertz" <mertz at gnosis.cx> wrote:
> I've got to thinking about multimethods/multiple dispatch lately.  I
> wonder if Pythonistas have some futher opinions on the use of these.

Theoretically multimethods are a great idea.  Being an elegant
generalization of object-orientation, having multimethods available
may save you from a clumsy workaround every once in a blue moon.

The problem is that designs that take full advantage of multimethods
are all too often bad.

> There are a few things that I had particularly in mind.  In reading
> about Dylan, I found that it has a method named 'next-method( )' to
> propogate dispatch from the closest match to more distant ones.  You can
> control whether you want this to happen by including that method, or
> not.  Take a look at:
> 
>     http://www.tpk.net/~ekidd/dylan/multiple-dispatch.html

Excellent example. 

I would describe the purpose of inspect-vehicle like this:
    Perform the relevant inspections sequentially.

which would naturally translate into:
    for inspection in vehicle.relevant_inspections():
        inspection.perform(vehicle)
        # or perhaps: vehicle.perform(inspection)

Unfortunately the next-method() example code looks nothing like this.
'relevant_inspections', instead of being a mallable data structure, is
hardwired into the class hierarchy.  You can't rearrange the order of
the inspections according to some criterion, you can't print a list of
inspections to be done, you can't collect a list of inspections that
failed without adding boilerplate code to each and every component
method, and you can't skip inspections already performed successfully.
All things that come naturally in the straightforward single-dispatch
solution.

- Anders






More information about the Python-list mailing list