Code generator and visitor pattern

Stefan Behnel stefan_ml at behnel.de
Thu Jul 15 15:10:58 EDT 2010


Karsten Wutzke, 15.07.2010 20:45:
> Well, I'm most experienced in OO, so writing OO in Python seems like
> the way to start with Python. The visitor pattern uses single-
> dispatch, that is, it determines which method to call be the type of
> object passed in.

Well, then do that. Put the types into a dict and map them to the functions 
to call, then call the function through the dict. That's a pretty common 
way to do dispatching.


> Note, that I have an hierarchical object structure which I want to
> iterate over, so using OO looked natural to me. If there's a better
> approach, I'm all ears.

You speak in riddles, but my guess is that your problem is that you don't 
want to dispatch mechanism to match only exact types but also subtypes. No 
problem, just build your dict incrementally and add new types as they come 
in. See this file for an example:

http://hg.cython.org/cython-devel/file/tip/Cython/Compiler/Visitor.py

Stefan




More information about the Python-list mailing list