[Cython] Visitor patterns and Python 2.4
Dag Sverre Seljebotn
dagss at student.matnat.uio.no
Wed May 7 08:30:47 CEST 2008
> We ship our own Python (2.5.2) with Sage, but I would rather stick
> with 2.3 compatibility. You might not consider it "important" but my
> computer (OS X 10.4, yet I know 10.5 is out but I haven't found the
> time to upgrade yet) has 2.3 by default, as did OS X 10.3.
>
It is certainly an argument I didn't consider.
Still... Python 2.4 was released 3 1/2 years ago, and I think Cython
users are going to be power users who will know how to upgrade their Python.
> I very much dislike using __class__.__name__ to decide what functions
> to call, but it doesn't look like you need decorators to get around
> this, do you?
>
Not having decorators makes the overhead of not using the function name
much greater. I.e. compare:
class MyVisitor(VisitorTransform):
@pre(cls=FuncDefNode)
def create_function_scope(self, node):
...
(or similar) with something like (changing the match function slightly)
class MyVisitor(VisitorTransform):
def create_function_scope(self, node):
...
create_function_scope = pre(cls=FuncDefNode, create_function_scope)
The latter one is so heavy to write that I'd rather want to simply do
class MyVisitor(VisitorTransform):
def pre_FuncDefNode(self, node):
....
however I don't think this is as nice. Yes, I know, it is the standard
visitor pattern, but I think that pattern is the way it is because it is
made for less powerful languages than Python; using a real class
references seems better than having (using __name__ or using a
convention or in some way add extra, additional information to care about).
Also using decorator opens the way for other types of matches (though I
won't propose a full XPath match again, perhaps simple "attr" matching,
i.e. to which attribute and parent does the node belong to:
@pre(parent_cls=FuncDefNode, attr="body")
...
--
Dag Sverre
More information about the Cython-dev
mailing list