> > I suspect, if you can be explicit about the goal you're aiming for with > this code, a better design can be found that doesn't require all those > polymorphism-breaking type checks. > It is difficult to explain what I am trying to do, but let me try. I am mapping data from one hierarchy into another. The mapping rules are quite complex. I developed a system such that the rules could be defined as "expressions" of the source hierarchy i.e a particular entry in the target hierarchy could be represented as an expression of entries in the source hierarchy. Suppose a point is stored in x, y coordinates in the source hierarchy, and in polar coordinates in the target hierarchy, I could write (forget for the moment what sourcePt is): pointMapping = { sourcePt.key : dict( radius = Sqrt(sourcePt.value['x']**2 + sourcePt.value['y']**2), angle = Atan(sourcePt.value['y']/sourcePt.value['x']), ), } The above dictionary is delay-evaluated. sourcePt is an instance of a class that facilitates the delayed evaluation. Sqrt, Atan etc. are wrappers to the math functions to facilitate delayed evaluation. When I encounter a point object, I could 'evaluate' the above mapping for the point object to get the target dictonary. The actual requirements are much more involved than this. The motivation of the design was to enable application developers (who are not experts in python) to be able to write the mappings. The mappings also need to be readable. You could consider this to be some sort of DSL. However, because of the number of rules involved, I am trying to be as close to Python expressions as possible. If the target setting is to be a tuple, for example, I want to be able to write the tuple directly as "( expr1, expr2 )", rather than, say, "Tuple(expr1, expr2)". There is also a requirement to validate the mapping on load so that run-time errors are minimized. The system we are developing is quite reusable and we have been able to use it for three different mappings so far. At this point, I am trying to profile the code and noticed that a non-trivial amount of time is being spent in the particular function I mentioned in this thread.
Regards, Krishnan -- http://mail.python.org/mailman/listinfo/python-list