PEP 318

Ville Vainio ville at spammers.com
Wed Mar 24 04:45:05 EST 2004


>>>>> "Andrew" == Andrew Bennetts <andrew-pythonlist at puzzling.org> writes:

    Andrew> But the decorator syntax doesn't help with this case at
    Andrew> all.

    Andrew> You *could* hack up multimethods today, though, by abusing
    Andrew> metaclasses:

Hmm? I would have figured that once you are able to associate
parameter types with a function name and a concrete callable the gets
called, you can implement multimethods.

The wrapper would essentially

1. Look up the function name N from func_name

2. Insert the mapping of parameter types to the callable in the
   data structure that holds the mappings for multimethod N

3. Return the dispatcher object that remembers it's a dispatcher for
   multimethod N, so, when called, it can search the data structure
   for its own multimethod for the concrete callable to invoke with
   the parameters.

This would obviously be used for CLOS-style multimethods, i.e. the
concrete methods would be plain functions. This wouldn't work for
operator overloading, for example, unless all the dispatcable classes
did a:

class C:
    def __mul__(self, other):
        return mm_multiply(self,other)


Where mm_multiply would be the multimethod. Perhaps a base class could
be introduced that did this for all the operators...

Again, I might be overlooking something, but I haven't yet figured out
what :).

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



More information about the Python-list mailing list