Python Macros

Jacek Generowicz jacek.generowicz at cern.ch
Wed Oct 6 09:43:21 CEST 2004


michele.simionato at gmail.com (Michele Simionato) writes:

> Jacek Generowicz <jacek.generowicz at cern.ch> wrote in message news:<tyfsm8td503.fsf at pcepsft001.cern.ch>...
> > _Most_ object-oriented languages support object orientation via the
> > message passing paradigm. Python included.
> 
> In my mind I have the equation "message passing = single dispatching"

I don't think that message passing implies single dispating by
necessity. After all, the visitor pattern allows you to hack in
multiple dispatch into message-passing languages. There must be other
ways too [1]

> as opposed to multiple dispatching (generic functions).

But it is true that generic functions' feature of not singling out the
first argument as special, both in terms of it's syntactic position,
and the "method ownership" concept, does make multiple dispatch seem
more natural.

> IOW, it seems to me that all OOP systems based on single dispatching
> are more or less the same and it is just a matter of terminology
> ("passing a message to an object" = "calling a method of an
> object").

> Am I right in this simplification? Or the terms has a much more
> precise meaning?

If someone can find The Canonical definition of message passing, then
I'd be interested to see it, but I fear that trying to nail down such
a definition could be a matter of considerable debate and controversy
(just as it is for "strong typing" and "object orientation" for
example).



[1] Something along these lines maybe, though I haven't got the time
    to develop it beyond a crappy idea (ie I haven't tried it, I'm
    sure there are mistakes, it's not very clever):

    class mdm(object):
        "Multiply dispatching method (message?)."
    
        def __init__(self):
            self.dispatch = {}
    
        def add(self, method, types):
            self.dispatch[types] = method
    
        def __call__(self, *args):
            return self.dispatch[self.map(type, args)](*args)
    
        def __get__(self, ...):
            # Method binding descriptor should go here
            # The details escape me at the moment
    
    class Foo(object):
    
        bar = mdm()
        def _bar(self, a,b):
            # whatever
        bar.add(_bar, (int, int))
        def _bar(self, a,b):
            # something else
        bar.add(_bar, (dict, str))
    
        del _bar


[ Orthogonal issue: Wouldn't it be great to have fully fledged
anonymous, inline functions? Then we wouldn't have to faff around with
"_bar" and "del _bar" and "bar.add", we could just write:

bar = mdm((type1, type2,
          lambda self, a, b: # whatever),

          (type3, type4,
          lambda self, a, b: # something else))

(Or, in this case, it could be cleaned up by smuggling in an
"accumulating dictionary", to pass to the metaclass ... as discussed
in another thread last week.)

]



More information about the Python-list mailing list