Who's minister of propaganda this week?

Alex Martelli aleaxit at yahoo.com
Thu Mar 15 05:26:52 EST 2001


"Neelakantan Krishnaswami" <neelk at alum.mit.edu> wrote in message
news:slrn9b0cc2.gsa.neelk at alum.mit.edu...
> On Thu, 15 Mar 2001 00:09:30 +0100, Alex Martelli <aleaxit at yahoo.com>
wrote:
> >"Steven D. Majewski" <sdm7g at virginia.edu> wrote in message
> >news:mailman.984595938.11127.python-list at python.org...
> >    [snip]
> >> ( OK: not having multiple dispatch in Python means that some manual
> >> dispatching is sometimes required, but usually this is burried deep
> >> in some classes __coerce__ method. )
> >
> > "Usually" in arithmetic, perhaps.  For fancier stuff, lack of
> > multimethods sometimes cramps (not that it cramps any less in C++,
> > mind you), though some patterns (such as 'dynamic visitor') can help
> > a bit in certain (reasonably frequent) special cases.
>
> What's a dynamic visitor? Is it a dictionary keyed on class? That's
> what I usually do when emulating multimethods in Python.

Sorry, the pattern-name is _Acyclic_ Visitor -- it's fully
described by its author, Robert Martin, in the PDF at URL
http://www.objectmentor.com/publications/acv.pdf for example.

The 'dynamic' aspect is at its core (in the C++ version, it
uses dynamic_cast systematically, for example) but not in
the name (because the key _goal_ of it, vs regular Visitor,
is framed as removing a very troublesome cyclic dependency).

Keying a dict on class has the unfortunate side-effect of
not allowing polymorphism by inheritance -- objects of classes
_derived from_ the one used as the key will not match.  You
can preserve this basic idea by using as key, instead of
the class, some ad-hoc attribute which will be inherited (if
it IS acceptable to alter the class-objects of interest by
adding an attribute) except where specifically overridden,
an approach which can be used as a first-path (fast-path)
to be followed by an isinstance loop (slow but sure) if and
when the key attribute is not obtained (e.g. for types that
are not classes, or, classes that can't be altered).

I don't think the applicability cases of this approach to
hand-simulation of multiple dispatch will overlap much
with those of visitors (of any kind) or coercion approaches.


Alex






More information about the Python-list mailing list