Multimethods 101 (was Re: Multiple dispatch (Re: Q: Python 2.0 preliminary features?))

Barry Margolin barmar at bbnplanet.com
Fri Oct 29 17:58:11 EDT 1999


In article <3819EA9D.6A46E845 at mail.earthlink.net>,
Charles Hixson  <charleshixsn at mail.earthlink.net> wrote:
>Louis Madon wrote:
>
>> """ snip """
>> In the beginning Greg Ewing wrote:
>>
>> > Suppose AA is a subclass of A, and BB is a subclass of B,
>> > and there are methods defined for the combinations
>> > foo(A, B), foo(AA, B) and foo(A, BB).
>> >
>> > Now, if you make a call with the combination foo(AA, BB),
>> > which method should get called?
>>
>> Several people said its "ambiguous".
>
>It's ambiguous unless you define what should happen.  To my mind the
>apporpriate call would be to foo (A,B), but any one of them would be a valid
>interpretation in the absence of language definitions.  Depending on search
>order, it would seem most Pythonish to call the foo (AA, B) method, since
>that would probably be the first one found along the left hand branch of the
>search path, and it does fit.

I think CLOS would also call that one by default.  It allows argument
priority order to be declared in the generic function declaration, so you
could make it select either foo(AA, b) or foo(A, BB).  Left-to-right is the
default.

One of the reasons why I think this hasn't been too much of a problem is
CLOS's :before and :after method types.  When composing classes, it's often
more common to add these secondary method types rather than to override the
primary method.  So you'll have a primary foo(A, B), and secondary foo(AA,
B) and foo(A, BB) methods that add their own functionality to the primary
method.  All of them will be called automatically.

-- 
Barry Margolin, barmar at bbnplanet.com
GTE Internetworking, Powered by BBN, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.




More information about the Python-list mailing list