Python complaints

Gareth McCaughan Gareth.McCaughan at pobox.com
Thu Dec 2 17:29:37 EST 1999


Robert Roy wrote:

[I said:]
>>  - Method definitions have to be lexically contained in
>>    class definitions. So suppose you write something that
>>    parses a language and then does various things with
>>    the parse tree; if you adopt an OO approach to the
>>    parse tree (with things like "class IfStatement(ParseNode):")
>>    then you can't separate out very different operations like
>>    foo.print(), foo.compile() and foo.evaluate(). (That is,
>>    you can't group all the print methods together, and group
>>    all the compile methods together somewhere else, etc.)
>>    You can, of course, split your code up by having the WhileLoop
>>    and UntilLoop classes in different files, but this seems
>>    less sensible to me. :-) (This particular gripe applies to
>>    most OO languages.)
>> 
> 
> Unless I misunderstood your post, the following code would do the job
> nicely.
> 
> bar.py
> def bar(self):
>     print self.__class__.__name__
>     
> def bar2(self):
>     print 'In bar 2',
>     self.bar
> 
> foo.py
> class foo:
>     from bar import *
[etc]

That allows me to split up a class's methods into several
different files, true. On the other hand, they can't then
be grouped in some other way.

What it would be nice to be able to do is to have, say,

class ParseNode
class WhileNode(ParseNode)
class IfNode(ParseNode)
class BlockNode(ParseNode)

with methods

in file compile.py:
  WhileNode::compile()
  IfNode::compile()
  BlockNode::compile()

in file evaluate.py:
  WhileNode::evaluate()
  IfNode::evaluate()
  BlockNode::evaluate()

and so on.

(You can sort of get around this by using what's called
"the Visitor pattern", but that (1) involves structuring
everything rather differently and (2) makes the code
less efficient. It might still be the best thing to do.)

-- 
Gareth McCaughan  Gareth.McCaughan at pobox.com
sig under construction




More information about the Python-list mailing list