how to dispatch objects depending on their class

Curzio Basso curzio.basso at unibas.ch
Wed Aug 11 06:30:34 EDT 2004


Peter Otten wrote:

> I'd say the choice of the dispatch mechanism - dictionary lookup vs Duncan
> Booth's name-mangling - is independent of the question whether the visitor
> pattern applies. Duncan's setup would then become something like
> 
> class VisitorMixin:
>     def accept(self, visitor):
>         visitor.dispatch[self.__class__](self)
> 
> class Visitor:
>     def __init__(self):
>         self.dispatch = {A: self.visit_A, B: self.visit_B}
>     def visit_A(self, a):
>         pass
>     def visit_B(self, b):
>         pass
> 

Yes, the dispatching would be not necessary if no mixin was used. Which 
is not desirable, however, because it would add a dependency between the 
subclass and the visitor.

thanks again, I got a lot of useful advices.

cheers, curzio



More information about the Python-list mailing list