Brian Beck wrote: >> >> class D1(Base): >> def foo(self): >> print "D1" >> >> class D2(Base): >> def foo(self): >> print "D2" >> obj = Base() # I want a base class reference which is polymorphic >> if (<need D1>): >> obj = D1() >> else: >> obj = D2() > > I have no idea what you're trying to do here and how it relates to > polymorphism. >
He's translating C++ code directly to Python. obj = Base() creates a variable of type Base(), to which you can assign different object types (D (), D2()) which implement the Base interface (are derived from Base). Err... At least I think it's what this code is supposed to mean... In C++ you'd do: Base *baseob; if( <i want d1> ) { baseob = (Base*)new D1(); } else { baseob = (Base*)new D2(); } baseob->foo(); (should, if foo is declared virtual in Base, produce "d1" for D1, and "d2" for D2) At least IIRC, it's been quite some time since I programmed C++... ;-) *shudder* --- Heiko. -- http://mail.python.org/mailman/listinfo/python-list