downcasting problem

Diez B. Roggisch deets at web.de
Mon Oct 25 16:26:17 EDT 2010


Nikola Skoric <nick at fly.srk.fer.hr> writes:

> 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.

This is usually called a 'reducer' in parsing, and it's purpose is to
rewrite the parsing result to something new.

It's usually implemented using a visitor-pattern.

In your case, it should be rather straightforward, by simply iterating
over the objects and creating new, more specialized types depending on
what you actually need.

So you don't need to delegate anything.

Diez



More information about the Python-list mailing list