[Compiler-sig] AST observations

Jeremy Hylton jeremy@zope.com
Thu, 18 Apr 2002 14:44:32 -0400


(I hope it's okay that I'm responding in little chunks.  There's been
a lot to digest.)

>>>>> "ECN" == Eric C Newton <ecn@metaslash.com> writes:

  ECN>         Setting the "visit" method on the Visitor is, ahem, a
  ECN>         novel approach.  It's a convention that PyChecker (the
  ECN>         use of, not the development of) doesn't like ("unknown
  ECN>         method visit()").  I don't know if I don't like it, but
  ECN>         it was unexpected.  Passing the walker to the dispatch
  ECN>         function is the sort of thing I would expect.

Ahem is a nicer word that sucks :-).

Anyway, it seemed like a clear delegation to me and the alternative
seemed to involve a lot of helper methods that didn't serve any
functional purpose and required visitors to inherit from a special
base class.  The joy of Python is not in writing reams of boring code,
or something like that.

I think the boring code would go something like this:

class VisitorBase:

      def __init__(self):
	  self._visit_hook = None # a hook for the walker
	  
      def register_walker(self, walker):
          self._visit_hook = walker.dispatch

      def unregister_walker(self):
          self._visit_hook = None

      def visit(self, *args, **kwds):
          self._visit_hook(*args, **kwds)

That's a dozen lines of code to do something that still needs to be
explained in the doc string.  So I figured, I'd just use an assignment
and explain it in the doc string.

Jeremy