Re: OO in Python? ^^
- From: Heiko Wundram <modelnine@xxxxxxxxxxxxxx>
- Date: Sun, 11 Dec 2005 00:40:09 +0100
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.
.
- Follow-Ups:
- Re: OO in Python? ^^
- From: Matthias Kaeppler
- Re: OO in Python? ^^
- References:
- OO in Python? ^^
- From: Matthias Kaeppler
- Re: OO in Python? ^^
- From: Brian Beck
- OO in Python? ^^
- Prev by Date: Re: Dectecting dir changes
- Next by Date: Re: OO in Python? ^^
- Previous by thread: Re: OO in Python? ^^
- Next by thread: Re: OO in Python? ^^
- Index(es):
Relevant Pages
|