> Hi everybody, > > I need to downcast an object, and I've read repeatedly that if you > need to downcast, you did something wrong in the design phase. So, > instead of asking how do you downcast in python, let me explain my > situation. > > I have a 2-pass parser. 1st pass ends up with a bunch of superclass > object, and 2nd pass is supposed to downcast those to one of > subclasses so I can work with them. > > Details: > > I have a file full of lines which I parse into Line objects. I also > have two subclasses of Line, namely Individual and Family. Constructor > of both subclasses needs all Line objects in the file to be > constructed, so I cannot construct subclass objects in the first pass. > > The Python way of doing this (as I understand it) would be to wrap a > subclass around Line and have the Line object as an attribute of a > subclass. Now, this obviously breaks polymorphism and I now have to > reimplement all of Line's classes in subclasses. Although > reimplementation is simple (def method(self): return > self.line.method()), that way of doing things is just not elegant.
> So, is there any more elegant solution to my problem? Why not have an abstract Line class with your Line methods, a SimpleLine (your current Line class, extends AbstractLine), and IndividualLine and FamilyLine (both extending AbstractLine)? Cheers, Emm -- http://mail.python.org/mailman/listinfo/python-list