downcasting problem

Nikola Skoric nick at
Mon Oct 25 16:06:19 CEST 2010

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

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.


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?

Thanks in advance.

"Now the storm has passed over me
I'm left to drift on a dead calm sea
And watch her forever through the cracks in the beams
Nailed across the doorways of the bedrooms of my dreams"

More information about the Python-list mailing list