UserLinux chooses Python as "interpretive language" of choice

Oren Tirosh oren-py-l at hishome.net
Sat Dec 20 11:24:27 EST 2003


On Sat, Dec 20, 2003 at 08:44:45AM -0500, John Roth wrote:
> I'm not certain I'm making the connection. The visitor pattern
> as I use it doesn't seem to have much to do with generators.
> It's a way of disconnecting the sequencing logic from the
> processing logic, 

You mean like this?

  def generator(...):
     sequencing logic, yield stuff

  for object in generator(...):
     processing logic 

> while a generator seems to encapsulate both in the same function.

Can you give any specific reasons why you believe this to be the 
case?

> A good case in point is file processing. I've got a class that
> encapsulates a directory: instantiate it and it reads the directory
> into an internal list. Then if you invoke the .visit(instance)) method,
> it calls either the 'file' or the 'directory' method in that instance
> for each of the names in the directory. If I need to go down a
> directory chain, I simply invoke it recursively.

Recursively? I thought the whole point of using such a class was to
disconnect, as you say, the sequencing logic from the processing logic.
If the user of the class must explicitly invoke the recursion the 
sequencing logic becomes hard-wired into the processing logic. With a 
generator you could pass an argument to the sequence logic that instructs 
it to do, for example, a breadth-first or depth-first traversal without 
affecting the processing logic.

> If I arranged it so I called the visitor instance one last time
> (possibly using a 'lastTime' method) I could completely

Like this?

  for object in generator(...):
     processing logic 
  last time


I think it's great that Python has functions and bound methods as
first-class objects and that they can be passed around as values. I just
don't think they should be used when there are far simpler ways to
achieve the same result.

   Oren





More information about the Python-list mailing list