declaring multimethods [Was: Re: PEP 318]

Ville Vainio ville at spammers.com
Wed Mar 24 13:28:30 EST 2004


>>>>> "Skip" == Skip Montanaro <skip at pobox.com> writes:

    Skip> always be the class).  Hmmm... t1 presents a problem since
    Skip> at the time multimethod(Matrix,Matrix) is called there is no
    Skip> Matrix class available (it hasn't been bound yet).  You'd
    Skip> have to fudge that and use strings as the type names.  That
    Skip> presents another problem.  Classes, unlike functions,

I'll reiterate here my view that putting the multimethods inside
classes is not really all that important. CLOS doesn't do that, for
one (And Lisp people certainly seem to be quite happy with that
approach :-). You can just do:

def multiply(self,other) [multimethod(Matrix, Vector)]:
    pass

after the classes have been created. The code within the function uses
self just like it would in a class.

With classes you get the advantage of having the names hidden in the
class namespace, obviously. But here I think the advantages of the
function-based approach outweigh the disadvantages. 

We could also specify that the first type listed is always the class
into which the multimethod should be injected, and do the injecting in
the decorator. It would look alien in python code to find the function
in a namespace different from where it was deffed, though.

FWIW, I'm gradually getting really excited about these wrapper
thingies; they seem to create a new dimension of pythonism, despite
the fact that they really bring nothing new to the table. I sense the
coming of a new success story a'la List Comprehensions.

-- 
Ville Vainio   http://tinyurl.com/2prnb



More information about the Python-list mailing list