Who's minister of propaganda this week?

Neelakantan Krishnaswami neelk at alum.mit.edu
Thu Mar 15 20:11:07 EST 2001


On Thu, 15 Mar 2001 11:26:52 +0100, Alex Martelli <aleaxit at yahoo.com> wrote:
>"Neelakantan Krishnaswami" <neelk at alum.mit.edu> wrote in message
>news:slrn9b0cc2.gsa.neelk at alum.mit.edu...
>>
>> 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.

Thanks for the link. 

> 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 actually have a function that walks the class tree checking it
against the dictionary, which solves this problem.  You don't need to
add a special attribute in Python since __class__ gives you what you
need for free. 

I only added it to my toolkit recently, though, sine I tend to use
classes like algebraic data types in ML or Haskell -- as tagged
variants rather than designed for user-extension. I find subclassing
friendly designs are a lot harder to design in a way that doesn't
reveal too much internal structure.

> 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.

I honestly haven't used Visitors very much, since most of the OO
languages I've used have either had high dynamism (like Python) or
built-in multimethods. C++ and Java are alien worlds to me. :)


Neel




More information about the Python-list mailing list